How to intercept WebRequest.Timeout

Hi, there

WebRequest wreq = WebRequest.Create(Url);
wreq.Timeout = 15000;

IAsyncResult r = (IAsyncResult) wreq.BeginGetResponse
.......
.......

I must do something when wreq.Timeout.
Where can I write code in

Thanks...


Answer this question

How to intercept WebRequest.Timeout

  • Doc Adams

    Thanks...

    If TimeOut occur in downloading,
    like receive 0 Byte over 15 seconds.

    Can I handle that in BeginRead()

  • Severcool

    Timeout doesn't work on Asynchronous calls.  However, you get lucky with this particular class and you have the ability to cancel requests using the Abort method.  Generally you'll want to start an asynchronous request, wait some set amount of time using a Sleep call, and then check your IAsyncResult to see if the operation has completed yet.  If it hasn't, then you can call Abort().  Something like this might work:


    IAsyncResult r = (IAsyncResult) wreq.BeginGetResponse();

    r.AsyncWaitHandle.WaitOne(15000, false);
    if ( !r.IsCompleted ) {
        wreq.Abort();
    }

  • saulius74

    After adding some extra wiring to your code, I'm not having any issues restarting the download.  Currently, you aren't setting the resume headers so it is a restart download rather than a resume download.  I am assuming you'll fix that after you get around any issues you are having though.

    I can't see any problems here.  I can hit the cancellation button as often as I like and no problems.  I might play around with this some tomorrow.  I honestly think an asynchronous download class with status events and resume functionality might be a cool ticket to write and I'm sure you would appreciate an extra pair of hands and eyes ;-)

  • alfredkoo

    Hi, Thanks a lot

    I know how to resume, I omit the resume code here,
    just to protrude the restart download error.

    If I run 10 times, it gets error about 8 times,
    Two times are successful.

    If I build the project to .EXE
    then run it not in VS-IDE.
    It gets error about 6 times,
    Four times are successful.

    If add the code "Thread.Sleep(5000);" to cancel button_click before call Download(),
    always are successful. Never get an error.


    private void button2_Click(object sender, System.EventArgs e)
    {
    rs.Request.Abort();
    rs.ResponseStream.Close();
    fs.Close();
    rs = null;

    Thread.Sleep(5000); // Add Sleep code here
    Download();
    }


    Thanks again.

  • Ahmedtelb

    Thanks a lot, you are really great.

    I can abort the WebRequest now,
    but when download once more after Abort()
    I got the WebException.
    It says the request was cancel.

    In download function, 
      WebRequest wreq = WebRequest.Create(...);

    The wreq is a new WebRequest, why it says the request was cancel

    Please help
    Thanks.

    [Justin Rogers: Added code brackets to post for clarity.]

    private RequestState rs = null;
    private FileStream fs = null;

    public class MainForm
    {
    private void Download()
    {

    label1.Text = "Downloading...";
    rs = new RequestState();

    WebRequest wreq = WebRequest.Create("http://download.nullsoft.com/winamp/client/winamp3_0-full.exe");
    rs.Request = wreq;

    IAsyncResult r = (IAsyncResult) wreq.BeginGetResponse(
    new AsyncCallback(RespCallback), rs);
    }

    private void RespCallback(IAsyncResult ar)
    {
    RequestState rs = (RequestState) ar.AsyncState;

    WebRequest req = rs.Request;
    WebResponse resp = req.EndGetResponse(ar);

    fs = new FileStream("C:\\" + "winamp3_0-full.exe", FileMode.Create, FileAccess.Write);

    Stream ResponseStream = resp.GetResponseStream();
    rs.ResponseStream = ResponseStream;

    IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, 
    1024, new AsyncCallback(ReadCallBack), rs);
    }

    private void ReadCallBack(IAsyncResult asyncResult)
    {
    RequestState rs = (RequestState)asyncResult.AsyncState;
    Stream responseStream = rs.ResponseStream;

    int read = responseStream.EndRead( asyncResult );
    if (read > 0)
    {
    fs.Write(rs.BufferRead, 0, read);

    IAsyncResult ar = responseStream.BeginRead( 
    rs.BufferRead, 0, 1024, 
    new AsyncCallback(ReadCallBack), rs);
    }
    else
    {
    label2.Text = "Finish";
    }
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    Download();
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    rs.Request.Abort();
    rs.ResponseStream.Close();
    fs.Close();
    rs = null;

    Download(); // Once more download here
    }
    }

    public class RequestState
    {
    const int BufferSize = 1024;
    public byte[] BufferRead;
    public WebRequest Request;
    public Stream ResponseStream;
          
    public RequestState()
    {
    BufferRead     = new byte[BufferSize];
    Request        = null;
    ResponseStream = null;
    }
    }

  • HCC

    Not sure what you mean here.  Maybe a small code snippet showing what you mean by using BeginRead would help.
  • How to intercept WebRequest.Timeout