webrequest.begingetresponse正在采取太多的时间,当网址无效 [英] webrequest.begingetresponse is taking too much time when the url is invalid

查看:162
本文介绍了webrequest.begingetresponse正在采取太多的时间,当网址无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的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 ),它会尝试,直到它得到超时。请参见以下链接 - <一href="http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/939e78bf-b9be-4d0b-894e-ae7f0d6013ff"相对=nofollow>链接1 和<一href="http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/2cb74a7e-6e8f-4d05-b86a-2401df5d2ed3/"相对=nofollow>链接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();
            }
        }

解决方案

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

这篇关于webrequest.begingetresponse正在采取太多的时间,当网址无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆