getResponse in c# not working. No response coming back

askeei picture askeei · May 23, 2011 · Viewed 9.2k times · Source

I have this code in C#:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 30000; 
request.Method = "POST"; 
request.KeepAlive = true;
request.AllowAutoRedirect = false;

Stream newStream = request.GetRequestStream();
newStream.Write(bPostData, 0, bPostData.Length);

byte[] buf = new byte[1025]; int read = 0; string sResp = "";
HttpWebResponse wResp = (HttpWebResponse)request.GetResponse();
Stream resp = wResp.GetResponseStream();

The line HttpWebResponse wResp =... just hangs (as in no response from the URL). I'm not sure where exactly its crashing (cause i dont even get an exception error). I tested the URL in IE and it works fine. I also checked the bPostData and that one has data in it. Where is it going wrong?

Answer

usr picture usr · May 24, 2011

Try closing the request stream in variable newStream. Maybe the API waits for it to be done.