简体   繁体   English

网址无效时,webrequest.begingetresponse花费了太多时间

[英]webrequest.begingetresponse is taking too much time when the url is invalid

I am using webrequest to fetch some image data. 我正在使用webrequest来获取一些图像数据。 The url may be invaild sometime. 该网址可能会在某个时间被盗用。 In case of invalid URL, begingetresponse is taking time equals to timeout period. 如果网址无效,则begingetresponse所花费的时间等于超时时间。 Also the control become unresponsive during that period. 在此期间,控件也变得无响应。 In other word the async callback is not working asynchronously. 换句话说,异步回调不是异步工作的。 Is this expected behaviour? 这是预期的行为吗?

try
                                {
                                    // Async requests 
                                    WebRequest request = WebRequest.Create(uri);
                                    request.Timeout = RequestTimeOut;
                                    RequestObject requestObject = new RequestObject();
                                    requestObject.Request = request;
                                    request.BeginGetResponse(this.ProcessImage, requestObject);
                                }
                                catch (Exception)
                                {
                                    ShowErrorMessage(uri);
                                }

 private void ProcessImage(IAsyncResult asyncResult)
        {            
            try
            {
                RequestObject requestObject = (RequestObject)asyncResult.AsyncState;
                WebRequest request = requestObject.Request;
                WebResponse response = request.EndGetResponse(asyncResult);

                Bitmap tile = new Bitmap(response.GetResponseStream());
                // do something
            }
            catch (Exception)
            {
                ShowErrorMessage();
            }
        }

looks like this is an issue with .NET. 看起来这是.NET的问题。 BeginGetResponse blocks until DNS is resolved. BeginGetResponse阻止,直到解析DNS。 In case of wrong URL (like http://somecrap ) it tries until it gets timeout. 如果URL错误(例如http:// somecrap ),它将尝试直到超时。 See the following links - link1 and link2 请参阅以下链接-link1link2

I just ran into this same situation. 我只是遇到了同样的情况。 While it's not a perfect workaround I decided to use the Ping.SendAsync() to ping the site first. 虽然这不是一个完美的解决方法,但我还是决定先使用Ping.SendAsync()对站点进行ping操作。 Good part is the async part return immediately. 好部分是异步部分立即返回。 Bad part is the extra step AND not all sites respond to Ping requests. 不好的部分是额外的步骤,而且并非所有站点都响应Ping请求。

public void Start(WatchArgs args)
{
        var p = new System.Net.NetworkInformation.Ping();
        args.State = p;
        var po = new System.Net.NetworkInformation.PingOptions(10, true);
        p.PingCompleted += new PingCompletedEventHandler(PingResponseReceived);
        p.SendAsync(args.Machine.Name, 5 * 1000, Encoding.ASCII.GetBytes("watchdog"), po, args);
}

private void PingResponseReceived(object sender, .PingCompletedEventArgs e)
{
    WatchArgs args = e.UserState as WatchArgs;
    var p = args.State as System.Net.NetworkInformation.Ping;
    p.PingCompleted -= new System.Net.NetworkInformation.PingCompletedEventHandler(HttpSmokeWatcher.PingResponseReceived);
    args.State = null;
    if (System.Net.NetworkInformation.IPStatus.Success == e.Reply.Status)
    {
        //  ... BeginGetResponse now
    }
    else
    {
        /// ... machine not available
    }
}

Just code and running for a day but initial result look promising. 只需编写代码并运行一天,但初步结果看起来很有希望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM