0

我遇到了一个问题,我的代码在 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 我做错了什么?有时我会从队列中得到错误的代理,所以我希望能够超时并跳过。

        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)
        { }
4

4 回答 4

2

我遇到了同样的问题。问题在于您的超时代码。你有:

request.Timeout = 15*1000;

你需要:

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

.Timeout 属性用于发送超时。但是,在发送请求并开始读取之后,请求会出现整体超时。它是通过 ReadWriteTimeout 属性设置的。

ReadWriteTimeout 的默认值为 5 分钟,这就是您看到您所看到的行为的原因。

于 2013-08-21T12:11:52.333 回答
1

由于您指定了 15 秒的超时时间,因此我假设您等待的时间超过了 15 秒。代码延迟的原因可能是它正在等待连接可用。等待连接所花费的时间与您的请求超时无关。

我相信默认情况下,.NET 只允许两个同时连接到“端点”(IP 地址)。这可以通过您的应用程序/网络配置进行配置:

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

但是,这可能无法解决问题,因为您正在与之通信的服务器可能只允许每个客户端的有限数量的连接。您的代理人也可能参与其中。

于 2012-05-02T21:20:46.640 回答
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 行(倒数第二行),它将在单独的线程上提交您的超时方法,因此即使您的其他请求由于无效的 IP 地址或主机而挂起,它也可以取消请求。

于 2012-06-26T16:02:48.677 回答
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;
}
于 2012-05-03T00:58:06.693 回答