HTTPWebRequest async time out

  • Thread starter Thread starter éric
  • Start date Start date
É

éric

How do I make HTTPWebRequest time out after 5 seconds when it is called
async?
setting req.TimeOut seems to do nothing
I tried to set a seperate timer but once the call to the server was made on
the thread there was no way to stop it...

Regards,

éric
 
With .NET CF SP2, the timeout seems to be working with async HttpWebRequest
(this can save you the timer management), but Request.Abort() doesn't seem
to stop the request and the application won't exit (using App.Exit()) until
the request actually finishes on the background.

-- Sam
 
from ms help files:
Remarks
The Timeout property indicates the length of time, in milliseconds, until
the request times out and throws a WebException. The Timeout property
affects only synchronous requests made with the GetResponse method. To time
out asynchronous requests, use the Abort method.

So I want to use abort... however once I make the req and there is no server
to be found... it searches for a minute before getting back so that I can
call Abort.

How can I Abort before that?

Regards,

éric
 
CF doesn't support aborting a thread and that's probably why the abort doesn't work here. Here's my testing program, I was able to get the control back after the request times out, but Request.Abort() didn't really do the job to clean up, at least the main application won't be able to exit right away (Application.Exit()). I had this problem several months ago and was hoping that .NET CF SP2 might solve the problem. Unfortunately.

public class AsyncHttpWebRequester
{
public static ResponsePackage SendRequest(string URL, int timeout)
{
ManualResetEvent manualEvent=new ManualResetEvent(false);

HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create(URL);
myHttpWebRequest.Timeout=timeout;

ResponsePackage respPackage=new ResponsePackage(myHttpWebRequest, manualEvent);

IAsyncResult asyncResult = myHttpWebRequest.BeginGetResponse(new System.AsyncCallback(AsyncCalledBack),respPackage);

respPackage.EventSystem.WaitOne();
return respPackage;
}


public static void AsyncCalledBack(IAsyncResult asyncResult)
{
ResponsePackage respPackage=asyncResult.AsyncState as ResponsePackage;
HttpWebResponse response=null;
StreamReader strReader=null;
try
{
if (respPackage!=null && respPackage.Request!=null)
{
response=(HttpWebResponse)(respPackage.Request.EndGetResponse(asyncResult));
respPackage.StatusCode=response.StatusCode;
if (respPackage.StatusCode==HttpStatusCode.OK)
{
strReader=new System.IO.StreamReader(response.GetResponseStream());
respPackage.ResultString=strReader.ReadToEnd();
}
}
}
catch (Exception ex)
{
//Debug.Message("Exception happened: "+ex.Message);
respPackage.ResultString=ex.Message;
respPackage.Request.Abort();
}
finally
{
if (strReader!=null)
strReader.Close();

if (response!=null)
response.Close();

respPackage.EventSystem.Set();

}
}
 
Thanks Sam,

At least I know I have done all I can

Regards,

Éric
CF doesn't support aborting a thread and that's probably why the abort doesn't work here. Here's my testing program, I was able to get the control back after the request times out, but Request.Abort() didn't really do the job to clean up, at least the main application won't be able to exit right away (Application.Exit()). I had this problem several months ago and was hoping that .NET CF SP2 might solve the problem. Unfortunately.

public class AsyncHttpWebRequester
{
public static ResponsePackage SendRequest(string URL, int timeout)
{
ManualResetEvent manualEvent=new ManualResetEvent(false);

HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create(URL);
myHttpWebRequest.Timeout=timeout;

ResponsePackage respPackage=new ResponsePackage(myHttpWebRequest, manualEvent);

IAsyncResult asyncResult = myHttpWebRequest.BeginGetResponse(new System.AsyncCallback(AsyncCalledBack),respPackage);

respPackage.EventSystem.WaitOne();
return respPackage;
}


public static void AsyncCalledBack(IAsyncResult asyncResult)
{
ResponsePackage respPackage=asyncResult.AsyncState as ResponsePackage;
HttpWebResponse response=null;
StreamReader strReader=null;
try
{
if (respPackage!=null && respPackage.Request!=null)
{
response=(HttpWebResponse)(respPackage.Request.EndGetResponse(asyncResult));
respPackage.StatusCode=response.StatusCode;
if (respPackage.StatusCode==HttpStatusCode.OK)
{
strReader=new System.IO.StreamReader(response.GetResponseStream());
respPackage.ResultString=strReader.ReadToEnd();
}
}
}
catch (Exception ex)
{
//Debug.Message("Exception happened: "+ex.Message);
respPackage.ResultString=ex.Message;
respPackage.Request.Abort();
}
finally
{
if (strReader!=null)
strReader.Close();

if (response!=null)
response.Close();

respPackage.EventSystem.Set();

}
}
 
Back
Top