Best approach to Timeout using HttpWebRequest.BeginGetResponse

瘦欲@ 提交于 2019-12-23 12:06:42

问题


HttpWebRequest.BeginGetResponse doesn´t respect any Timeout properties from HttpWebRequest(Timeout or ReadWriteTimeout).

I read some approaches to get the same results, but I don't know if it's the best way to do it and if I should use for few calls or I can scale it inside loops(I am doing a webcrawler).

The important thing is, initially my code isn´t async, I just need async because my method should accept a CancellationToken.

My concern is about WaitHandles and ThreadPool.RegisterWaitForSingleObject. It isn´t a daily code then I don´t know if I can have problems in the near future.

private static void HandleCancellation(HttpWebRequest request, IAsyncResult getResponseResult, CancellationToken cancellationToken)
{
    using (WaitHandle requestHandle = getResponseResult.AsyncWaitHandle)
    {
        ThreadPool.RegisterWaitForSingleObject(requestHandle, TimeoutCallback, request, request.Timeout, true);

        //If request finish or cancellation is called
        WaitHandle.WaitAny(new[] {requestHandle, cancellationToken.WaitHandle});
    }

    //If cancellation was called
    if (cancellationToken.IsCancellationRequested)
    {
        request.Abort();
        cancellationToken.ThrowIfCancellationRequested();
    }
}

Calling(again, it isn´t async)

IAsyncResult getResponseResult = request.BeginGetResponse(null, null);

HandleCancellation(request, getResponseResult, cancellationToken);

return (HttpWebResponse)request.EndGetResponse(getResponseResult);

Reference: Better approach in management of multiple WebRequest


回答1:


The MSDN documentation for BeginGetResponse has a very good example of how to handle timeouts. It worked quite well for me in my Web crawler.



来源:https://stackoverflow.com/questions/10497853/best-approach-to-timeout-using-httpwebrequest-begingetresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!