(Http)WebRequest.GetRequestStream() doesn't return anything back


#1

Hi there,

My code has not been changed and this worked fine for long time but now users cannot register their accounts (adding it to our system). Here is the method:
public string DoWebRequest(HTTP_Method Method, string URI, string PostData)
{
string webResponseData = string.Empty;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(“http://twitter.com/oauth/authorize”);
webRequest.Method = Method.ToString();
webRequest.ProtocolVersion = HttpVersion.Version10;
webRequest.ServicePoint.Expect100Continue = false;
webRequest.UserAgent = string.Format(CultureInfo.InvariantCulture, “Twitterizer/{0}”, “1.0.0.0”); ;
webRequest.Timeout = 20000;
try
{
//PostDate value is:
//authenticity_token=5cf84dcaa35a5dc24ce6a601443bb256a1d9e761 &oauth_token=rACZqKqB1dIs3Aadf1SyGHRikcS9MLcFET32I2fRp8
//&session%5Busername_or_email%5D=[email address]&session%5Bpassword%5D=[account password]
if (!string.IsNullOrEmpty(PostData))
{
Stream dataStream = null;
if (Method == HTTP_Method.POST)
{
byte[] byteArray = Encoding.UTF8.GetBytes(PostData);
webRequest.ContentType = “application/x-www-form-urlencoded”;
webRequest.ContentLength = byteArray.Length;
dataStream = webRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
dataStream = null;
}
}
webResponseData = GetWebResponse(webRequest);
webRequest = null;
}
catch (Exception ex)
{
_errorLogs = _errorLogs + " # " + ex.Message;
webResponseData = null;
}
return webResponseData;
}

In above code, dataStream doesn’t return anything and therefore, the next statement (dateStream.write()) throw an exception.

Does anyone knows of a recent change that causing this code to break?

Thx,
Ben


#2

This code uses out dated paths (see the OAuth documentation for updated OAuth paths – mostly you need to switch to the api subdomain). Additionally, it’s sending a bunch of parameters that have no relation to the API: authenticity_token, &session%5Busername_or_email%5D=[email address]&session%5Bpassword%5D=[account password]

What is it you’re trying to accomplish?


#3

Thx for the answer. It’s probable. This code has been there and the developer gone and I am new with no experience with Twitter.

Anyway, we have a marketing automation tool that one of the elements is Twitter that campaigners can send tweet or direct message to their contacts. Before this can happen, the campaigner should add a Twitter account to our system, in which he/she going to send messages from.

This is the part that is broken (we call it provisioning). Otherwise , with existing accounts, it is still possible to send the tweet. Perhaps this is to validate the senders but I am not sure how whole this procedure works or should work.


#4

OK. I now understood what that piece of code is doing. It sends a request to twitter for that account to authorize the use of our application. If that is successful, the twitter returns a PIN.

I went through this manually by typing the same URL and could get the pin after entering my username n password. So, the URL is working but still possible that it doesn’t through API.

Hope this helps more to resolve my problem.


#5

It’s likely you can solve this particular hurdle by replacing this line:

(HttpWebRequest)WebRequest.Create("http://twitter.com/oauth/authorize")

with

(HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth/authorize")

I would validate any other place in your code that uses http://twitter.com/oauth/* paths and make sure they’re using https://api.twitter.com/oauth/* instead.

Chances are that your code is also leveraging API v1 – which is deprecated and will be retired on May 7. You may need to do additional work to keep yourself running beyond May 7. Take a look at [node:16293] for more info.


#6