2012-12-19 134 views
12

我爲程序做了一個函數,當請求類型爲GET時,如果它是POST,它會工作,它總是會產生一個超時異常(並且超時時間不會達到50s)行HttpWebResponse response = (HttpWebResponse)request.GetResponse();
我嘗試了很多事情,但我不知道爲什麼,可能有人在這裏知道。

編輯:得到它的工作,如果有人有興趣:https://gist.github.com/4347248

任何幫助將不勝感激。

我的代碼是:request.GetResponse總是給出一個超時

public ResRequest request(string URL, RequestType typ, CookieCollection cookies, string postdata ="", int timeout= 50000) 
    { 
     byte[] data; 
     Stream req; 
     Stream resp; 

     HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest; 
     request.Timeout = timeout; 
     request.ContinueTimeout = timeout; 
     request.ReadWriteTimeout = timeout; 
     request.Proxy = new WebProxy("127.0.0.1", 8118); 
     request.Headers.Add(HttpRequestHeader.AcceptLanguage, "de"); 
     request.Headers.Add("UA-CPU", "x86"); 


     request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); 
     request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618) "; 
     request.CookieContainer = new CookieContainer(); 
     request.CookieContainer.Add(cookies); 
     if (typ == RequestType.POST) 
     { 
      data = System.Text.Encoding.Default.GetBytes(postdata); 
      request.Method = "POST"; 
      request.ContentType = "application/x-www-form-urlencoded"; 
      request.ContentLength = data.Length; 
      req = request.GetRequestStream();//after a few tries this produced a Timeout error 
      req.Write(data, 0, data.Length); 
      req.Close(); 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();//This line produces a Timeout Exception 
      resp = response.GetResponseStream(); 

      if ((response.ContentEncoding.ToLower().Contains("gzip"))) 
      { 
       resp = new System.IO.Compression.GZipStream(resp, System.IO.Compression.CompressionMode.Decompress); 
      } else if ((response.ContentEncoding.ToLower().Contains("deflate"))) { 
       resp = new System.IO.Compression.DeflateStream(resp, System.IO.Compression.CompressionMode.Decompress); 
      } 

      return new ResRequest() { result = new System.IO.StreamReader(resp, System.Text.Encoding.UTF8).ReadToEnd(), cookies = response.Cookies, cstring = cookiestring(response.Cookies) }; 

     } 
     else 
     { 
      request.Method = "GET"; 

      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      resp = response.GetResponseStream(); 

      if ((response.ContentEncoding.ToLower().Contains("gzip"))) 
      { 
       resp = new System.IO.Compression.GZipStream(resp, System.IO.Compression.CompressionMode.Decompress); 
      } 
      else if ((response.ContentEncoding.ToLower().Contains("deflate"))) 
      { 
       resp = new System.IO.Compression.DeflateStream(resp, System.IO.Compression.CompressionMode.Decompress); 
      } 

      return new ResRequest() { result = new System.IO.StreamReader(resp, System.Text.Encoding.UTF8).ReadToEnd(), cookies = response.Cookies, cstring = cookiestring(response.Cookies) }; 
     } 

    } 
+0

「LITTLE」函數??? –

+0

確定它不少...我刪除了它 – Tearsdontfalls

+0

你爲「超時」設置了什麼? – VladL

回答

16

所以它在req.GetRequestStream()每次掛,或者它的工作原理「試了幾次」,然後掛?

如果它工作了幾次然後掛起,可能是因爲沒有正確關閉請求,這會導致連接用完。請確保Close()和/或Dispose()HttpWebResponse對象以及您正在創建的所有流和讀者。

+0

每當我做一個'req.GetRequestStream()'時它就會掛起。謝謝,但它沒有工作。我還發現,當我沒有傳遞任何Post參數到請求它將連接並返回500服務器錯誤 – Tearsdontfalls

+0

什麼是'data.Length'?你想發送一些非常大的東西嗎? –

+1

根據幾個示例代碼重寫了函數之後,第三個請求之後的每個請求均失敗。感謝您的提示,我在'HttpWebRespose'和'Stream'中添加了'Close();'和'Dispose();'現在一切都很好。代碼:https://gist.github.com/4347248 – Tearsdontfalls

1

您必須使用 response.Dispose();

方法結束