2012-07-29 67 views
4

幫助...我找不出爲什麼HttpWebRequest.GetRequestStream()拋出異常 異常......它總是在嘗試建立第二個(和後續)使用POST的 連接。基本上,我試圖每30秒創建一次連接 。下面是培訓相關代碼:第二次調用HttpWebRequest.GetRequestStream()拋出超時異常

我的主循環:

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); 
} 

從用戶連接:

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"); 
    . 
    . 
    . 
} 

以下異常是發生在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 

這裏是GetPage代碼問題所在:

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; 
     } 
    } 
} 

任何想法!?!第一次迭代總是很好。如果我終止程序並重新開始第一次迭代再次良好......它總是第二次和以後的迭代。

回答

23

這可能是問題的一部分:

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

在這裏,我們不處置的響應......所以它不是返回到連接池。

這不是真的清楚這種方法的意思做,但你應該處置的響應:

using (req.GetResponse()) {} 
+1

你是男人!應該用自己的東西保存4個小時,然後馬上來。 在這裏,我只需要建立一個連接來初始化一些cookie,以便我能夠在發佈請求期間登錄。 – Cailen 2012-07-29 19:33:58

+0

@ Cailen:如果您稍後在發送「真實」請求時設置它們,它不會初始化Cookie嗎?也許不是... – 2012-07-29 19:45:50

+0

謝謝喬恩,這真是太棒了。 – 2013-07-18 15:06:10

1

我也有類似的問題,但我明確地想等待之前退出的方法響應。我將請求的超時設置爲100毫秒,這似乎爲我解決了這個問題。

相關問題