The second call to HttpWebRequest.GetRequestStream () throws a timeout exception

Help ... I can’t understand why HttpWebRequest.GetRequestStream () throws a timeout exception ... it ALWAYS occurs when trying to set the second (and subsequent) using POST. Basically, I try to create a connection every 30 seconds. Here is the relevant code:

My main loop:

for( int i = 0; i < students.Count; i++ ) { Log( "Processing: " + students[i].DbID + ";" + students[i].Username + ";" + students[i].Password ); UserConnection uc = new UserConnection( students[i] ); uc.ParseAll(); Log( "Done Processing: " + students[i].DbID + ";" + students[i].Username + ";" + students[i].Password ); } 

From user:

 public UserConnection( Student student ) { this.student = student; this.cookies = new CookieContainer(); this.InitCookies(); this.courses = new List<Course>(); } private void InitCookies() { HttpWebRequest req = (HttpWebRequest)WebRequest.Create( baseUri ); req.Method = "GET"; req.CookieContainer = this.cookies; req.GetResponse(); } public void ParseAll() { ParseCourseInfo(); //get info for each class . . . } private void ParseCourseInfo() { HtmlDocument page = GetPage( "POST", homeUri, "username=" + student.Username + "&password=" + student.Password + "&testcookies=1" ); . . . } 

The following exception occurs in GetPage:

 29/07/2012 1:04:22 PM : Exception: System.Net.WebException Message: The operation has timed out Source: System Stack Trace: at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at URCoursesParserV2.UserConnection.GetPage(String method, Uri pageUri, String queryString) in UserConnection.cs:line 167 

Here is the GetPage code where the problem is:

 private HtmlDocument GetPage( string method, Uri pageUri, String queryString = "" ) { Stream data = null; HttpWebResponse res = null; HttpWebRequest req = null; try { req = (HttpWebRequest)WebRequest.Create( pageUri ); req.CookieContainer = this.cookies; req.Timeout = 1000 * 10; //10 seconds - I've also tried leaving it as default req.KeepAlive = false; ////I've also tried leaving it as true. if( method.ToUpper() == "POST" ) { req.Method = "POST"; if( queryString != "" ) { byte[] postBytes = Encoding.UTF8.GetBytes( queryString ); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = postBytes.Length; using( data = req.GetRequestStream() ) { //////////////EXCEPTION HERE ON SECOND ITERATION data.Write( postBytes, 0, postBytes.Length ); data.Close(); } } } else if( method.ToUpper() == "GET" ) { req.Method = "GET"; if( queryString != "" ) { pageUri = new Uri( pageUri.ToString() + '?' + queryString ); } } else { return null; } HtmlDocument page = null; using( res = (HttpWebResponse)req.GetResponse() ) { using( data = res.GetResponseStream() ) { page = new HtmlDocument(); page.Load( data ); } } return page; } catch(WebException e) { URCoursesParser.Log( e ); return null; } catch( Exception e ) { URCoursesParser.Log( e ); return null; } finally { ///data and res probably don't need to be checked here now because of 'using' blocks if( data != null ) { data.Close(); data = null; } if( res != null ) { res.Close(); res = null; } if( req != null ) { req.Abort(); req = null; } } } 

Any ideas!?! The first iteration is always beautiful. If I exit the program and restart the first iteration, then it will be fine again ... This is always the second and subsequent iteration.

+4
source share
2 answers

This may be part of the problem:

 private void InitCookies() { HttpWebRequest req = (HttpWebRequest)WebRequest.Create( baseUri ); req.Method = "GET"; req.CookieContainer = this.cookies; req.GetResponse(); } 

Here you are not getting rid of the answer ... therefore, it does not return a connection to the pool.

It is not entirely clear what this method should do, but you should get rid of the answer:

 using (req.GetResponse()) {} 
+23
source

I had a similar problem, but I clearly wanted to exit the method before waiting for an answer. I set the request timeout to 100 milliseconds and this seems to have fixed it for me.

+1
source

All Articles