Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest doesn't work except when fiddler is running

Tags:

This is probably the weirdest problem I have run into. I have a piece of code to submit POST to a url. The code doesn't work neither throws any exceptions when fiddler isn't running, However, when fiddler is running, the code posts the data successfuly. I have access to the post page so I know if the data has been POSTED or not. This is probably very non-sense, But it's a situation I am running into and I am very confused.

byte[] postBytes = new ASCIIEncoding().GetBytes(postData); HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://myURL); req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3"); req.Headers.Add("Accept-Language", "en-US,en;q=0.8"); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = postBytes.Length; req.CookieContainer = cc; Stream s = req.GetRequestStream(); s.Write(postBytes, 0, postBytes.Length); s.Close(); 
like image 780
deadlock Avatar asked Jan 26 '11 03:01

deadlock


1 Answers

If you don't call GetResponseStream() then you can't close the response. If you don't close the response, then you end up with a socket in a bad state in .NET. You MUST close the response to prevent interference with your later request.

like image 150
EricLaw Avatar answered Oct 02 '22 07:10

EricLaw