webrequest.begingetresponse正在采取太多的时间,当网址无效太多、网址、时间、webrequest

2023-09-04 00:51:54 作者:归途的路

我使用的WebRequest来获取一些图像数据。该URL可能有时invaild。如果无效的URL,begingetresponse需要时间等于超时时间。另外,控制成为在此期间没有响应。换句话说异步回调不工作异步。这是预期的行为?

 尝试
                                {
                                    //异步请求
                                    WebRequest的请求= WebRequest.Create(URI);
                                    request.Timeout =将requestTimeout;
                                    RequestObject requestObject =新RequestObject();
                                    requestObject.Request =请求;
                                    request.BeginGetResponse(this.ProcessImage,requestObject);
                                }
                                赶上(例外)
                                {
                                    ShowErrorMessage(URI);
                                }

 私人无效processImage来(IAsyncResult的asyncResult)
        {
            尝试
            {
                RequestObject requestObject =(RequestObject)asyncResult.AsyncState;
                WebRequest的要求= requestObject.Request;
                WebResponse的响应= request.EndGetResponse(asyncResult);

                位图瓦=新位图(response.GetResponseStream());
                // 做一点事
            }
            赶上(例外)
            {
                ShowErrorMessage();
            }
        }
 

解决方案

看起来这是一个问题与.NET。 BeginGetResponse块,直到DNS解析。如果错误的URL(如的http:// somecrap ),它会尝试,直到它得到超时。请参见以下链接 - 链接1 和链接2

I am using webrequest to fetch some image data. The url may be invaild sometime. In case of invalid URL, begingetresponse is taking time equals to timeout period. 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();
            }
        }
加拿大官宣的13大类移民项目

解决方案

looks like this is an issue with .NET. BeginGetResponse blocks until DNS is resolved. In case of wrong URL (like http://somecrap) it tries until it gets timeout. See the following links - link1 and link2