2012-05-02 97 views
0

我遇到問題,我的代碼等待很長一段時間 HttpWebResponse response = (HttpWebResponse)request.GetResponse();我在做什麼錯了?有時我會從我的隊列中弄到不好的代理,所以我想能夠超時並跳過。等待GET回覆

 Boolean match = false; 
     String clean = String.Empty; 
     String msbuff; 
     IWebProxy insideProxy; 
     try 
     { 
      //grab proxies based on queue 
      String proxyQueItem = Proxy_Que(_rankingConnectionString); 
      insideProxy = new WebProxy(proxyQueItem); 

      HttpWebRequest request = (HttpWebRequest) WebRequest.Create(ResultURL); 
      request.Proxy = insideProxy; 
      request.Timeout = 15*1000; 

      using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) 
      { 
       if (response.StatusCode == HttpStatusCode.OK) 
       { 
        Stream streamResponse = response.GetResponseStream(); 
        StreamReader streamRead = new StreamReader(streamResponse); 
        msbuff = streamRead.ReadToEnd(); 
        //get rid of , -, (,) 
        msbuff = msbuff.Replace(" ", String.Empty).Replace(" ", String.Empty); 
        msbuff = msbuff.Replace("-", String.Empty); 
        msbuff = msbuff.Replace("(", String.Empty); 
        msbuff = msbuff.Replace(")", String.Empty); 
        //attempt to find phone number 
        match = msbuff.Contains(Listing.PhoneNumber); 
        streamRead.Close(); 
       } 

      } 
     } 
     catch (Exception lk) 
     { } 
+0

您沒有使用[WebClient](http://msdn.microsoft.com/zh-cn/library/system.net.webclient.aspx)(.NET 1.1 - 4.0)或[HttpClient]( http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx)(.NET 4.5)? – dtb

+0

我也試過WebClient,它會在客戶端上等待。downloadString –

+0

您是否在問一個特定的錯誤,如果是,錯誤是什麼?或者你是否要求提供有關流量控制的建議,以便跳到隊列中的下一個代理? –

回答

0

如果您有一個代理隊列,並且您只需要超時並且在不好的情況下跳到下一個代理,那麼您可以循環直到獲得響應,捕獲錯誤並從隊列中獲取新的代理通過時間。像這樣的東西...

private string requestByProxy(string url) 
{ 
    string responseString, proxyAddress; 

    while (responseString == null) 
    { 
     try 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
      // set properties, headers, etc 

      proxyAddress = getNextProxy(); 

      if (proxyAddress == null) 
       throw new Exception("No more proxies"); 

      request.Proxy = new WebProxy(proxyAddress); 
      request.Timeout = 15 * 1000; 

      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      StreamReader sr = new StreamReader(response.GetResponseStream()); 
      responseString = sr.ReadToEnd(); 
     } 
     catch (WebException wex) 
     { 
      continue; 
     } 
    } 

    return responseString; 
} 
1

由於您指定了15秒的超時時間,我假設您等待的時間超過了15秒。代碼延遲的原因可能是它正在等待連接可用。等待連接所用的時間與請求的超時無關。

我相信,默認情況下,.NET只允許兩個同時連接到「端點」(IP地址)。這可以通過您的應用程序/ Web配置進行配置:

<system.net> 
    <connectionManagement> 
    <add address="www.example.com" maxconnection="10"/> 
    </connectionManagement> 
</system.net> 

然而,這可能解決不了問題,因爲你與之通信的服務器可能只允許每個客戶端連接的數量有限。您的代理也可能涉及。

+0

我不確定這是否是問題。我認爲這個問題與一個糟糕的代理有關,導致getresponse掛起4-5分鐘。 –

1

我遇到了問題之前,一個簡單的更改超時設置修復它。這是VB.NET而不是C#,但你應該能夠弄清楚。首先,指定第二超時方法,其可螺紋:

Private Sub TimeoutCallback(ByVal state As Object, ByVal timedOut As Boolean) 
     If timedOut Then 
      Dim request As Net.HttpWebRequest = state 
      If Not (request Is Nothing) Then 
       request.Abort() 
      End If 
     End If 
    End Sub 

然後,當你建立你的請求,並回讀,你可以設置是這樣的:

Try 
      Dim myHttpWebRequest1 As Net.HttpWebRequest 
      ' Create a new HttpWebrequest object to the desired URL. 
      myHttpWebRequest1 = CType(WebRequest.Create(String.Format("http://{0}:{1}", inURL, inPORT)), Net.HttpWebRequest) 

      ' Create an instance of the RequestState and assign the previous myHttpWebRequest1 
      ' object to it's request field. 
      Dim myRequestState As New RequestState() 
      myRequestState.request = myHttpWebRequest1 
      ' Start the Asynchronous request. 
      allDone.Reset() 
      Dim result As IAsyncResult = CType(myHttpWebRequest1.BeginGetResponse(AddressOf RespCallback, myRequestState), _ 
               IAsyncResult) 
      ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, _ 
                New WaitOrTimerCallback(AddressOf TimeoutCallback), _ 
                myHttpWebRequest1, DefaultTimeout, True) 
      allDone.WaitOne() 

通知的TheadPool線(2號從底部),它會將你的超時方法提交給一個單獨的線程,這樣它可以取消請求,即使你的其他請求由於無效的IP地址或主機而被掛起。

2

我遇到了同樣的問題。問題在於你的超時代碼。您有:

request.Timeout = 15*1000; 

您需要:

request.Timeout = 15*1000; 
request.ReadWriteTimeout = 15*1000; 

的.Timeout屬性是在發送超時。但是,在發送請求並開始讀取之後,請求的總體超時。它通過ReadWriteTimeout屬性設置。

ReadWriteTimeout的默認值是5分鐘,這就是您看到您所看到的行爲的原因。