正在等待 GET 响应

本文关键字:响应 GET 在等待 | 更新日期: 2023-09-27 18:32:54

我遇到了一个问题,我的代码等待了很长时间 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)
        { }

正在等待 GET 响应

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

request.Timeout = 15*1000;

你需要:

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

这。超时属性用于发送时的超时。 但是,在发送请求并开始读取后,请求的总体超时。 它是通过 ReadWriteTimeout 属性设置的。

读写超时的默认值为 5 分钟,这就是您看到所见行为的原因。

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

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

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

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

之前遇到了这个问题,对超时设置的简单更改解决了它。 这不是 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 地址或主机而挂起,它也可以取消请求。

如果你有一个代理队列,你只需要超时,如果它不好,你只需要跳到下一个,那么你可以循环直到你得到响应,捕获错误并每次从队列中获取一个新的代理。像这样的东西...

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