1

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();
            }
        }
malay
  • 1,434
  • 4
  • 17
  • 28
  • No, expected behaviour would be that the UI remains responsive if you use BeginGetResponse. Could you show us a snippet of the code where you call BeginGetResponse? – bernhof Aug 05 '09 at 09:51
  • Do you have any way to validate that url before you send it? I presume that the server you're requesting is a black box for you, don't have access to that code? – Sebastian Aug 05 '09 at 12:27
  • I think I have to validate the URL before using rather than blindly using the URL. Have to write more code :( – malay Aug 05 '09 at 12:44

2 Answers2

2

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

malay
  • 1,434
  • 4
  • 17
  • 28
1

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. Good part is the async part return immediately. Bad part is the extra step AND not all sites respond to Ping requests.

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.

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43