Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Async HttpWebRequest

Reply
Thread Tools

Async HttpWebRequest

 
 
APA
Guest
Posts: n/a
 
      06-11-2008
I've seen the MS sample async web request pattern and I ask is it really async if it is using a ManualResetEvent and setting WaitOne()? The
ManualResetEvent object is being declared as a static variable so isn't it causing problems with other threads that may be using the same class to
execute the async web request? If I remove the ManualResetEvent object will it be truly asynchronous and will I be losing something? Also, in my app
the web request is posting data.

For those that haven't seen the code it looks something like this:


public class AsyncWebRequest

public AsyncWebRequest(){}

public static ManualResetEvent allDone = new ManualResetEvent(false);

const int BUFFER_SIZE = 1024;

public void WebReq(string URL, string somedata)
{
try
{

if (URL.Trim() != "")
{
byte[] data = Encoding.ASCII.GetBytes(somedata);

HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(URL);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = data.Length;

ManualResetEvent WaitHndle = new ManualResetEvent(false);
RequestState rs = new RequestState(WaitHndle,this.Debug);

rs.Request = webRequest;
rs.DataToSend = data;

webRequest.BeginGetRequestStream(new AsyncCallback(ReadCallbackRequest), rs);

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

ThreadPool.RegisterWaitForSingleObject(r.AsyncWait Handle, new WaitOrTimerCallback(ScanTimeoutCallback), rs, (30 * 1000), true);

allDone.WaitOne();
rs.ResponseStream.Close();

}
}
catch (WebException ex) { string str = ex.ToString(); }
catch (Exception ex) { string sr = ex.ToString(); }
}


private static void RespCallback(IAsyncResult ar)
{
RequestState rs = (RequestState)ar.AsyncState;
try
{
WebRequest req = rs.Request;

WebResponse resp = req.EndGetResponse(ar);

Stream ResponseStream = resp.GetResponseStream();

rs.ResponseStream = ResponseStream;
rs.Waitone.Set();

if (rs.Debug)
{
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
}
}
catch (Exception ex)
{
rs.Waitone.Set();
}
}


private static void ReadCallbackRequest(IAsyncResult asynchronousResult)
{
RequestState rs = (RequestState)asynchronousResult.AsyncState;
try
{
HttpWebRequest request = rs.Request;

Stream postStream = request.EndGetRequestStream(asynchronousResult);

postStream.Write(rs.DataToSend, 0, rs.DataToSend.Length);
postStream.Close();
}
catch (Exception ex)
{
}
allDone.Set();

}

private static void ScanTimeoutCallback(object state, bool timedOut)
{
if (timedOut)
{
RequestState reqState = (RequestState)state;
if (reqState != null)
reqState.Request.Abort();
}
}

public class RequestState
{
const int BufferSize = 1024;
public byte[] DataToSend;
public StringBuilder RequestData;
public byte[] BufferRead;
public HttpWebRequest Request;
public Stream ResponseStream;
public ManualResetEvent Waitone;
public bool Debug;

public Decoder StreamDecode = Encoding.UTF8.GetDecoder();

public RequestState(ManualResetEvent waitHandle,bool debug)
{
BufferRead = new byte[BufferSize];
RequestData = new StringBuilder(String.Empty);
Request = null;
ResponseStream = null;
Waitone = waitHandle;
Debug = debug;
}
}
}
 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      06-11-2008
normally you would put code you want to happen during the async request
before the wait. then after the wait, you put the code to handle the response.

the question is what do you want the thread to do during the request call?
after?

note: this code will not work from an asp.net application. if a webform, use
the builting async processing, if called from a web service, you will want to
use a WaitHandle rather than ManualResetEvent.

-- bruce (sqlwork.com)


"APA" wrote:

> I've seen the MS sample async web request pattern and I ask is it really async if it is using a ManualResetEvent and setting WaitOne()? The
> ManualResetEvent object is being declared as a static variable so isn't it causing problems with other threads that may be using the same class to
> execute the async web request? If I remove the ManualResetEvent object will it be truly asynchronous and will I be losing something? Also, in my app
> the web request is posting data.
>
> For those that haven't seen the code it looks something like this:
>
>
> public class AsyncWebRequest
>
> public AsyncWebRequest(){}
>
> public static ManualResetEvent allDone = new ManualResetEvent(false);
>
> const int BUFFER_SIZE = 1024;
>
> public void WebReq(string URL, string somedata)
> {
> try
> {
>
> if (URL.Trim() != "")
> {
> byte[] data = Encoding.ASCII.GetBytes(somedata);
>
> HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(URL);
> webRequest.Method = "POST";
> webRequest.ContentType = "application/x-www-form-urlencoded";
> webRequest.ContentLength = data.Length;
>
> ManualResetEvent WaitHndle = new ManualResetEvent(false);
> RequestState rs = new RequestState(WaitHndle,this.Debug);
>
> rs.Request = webRequest;
> rs.DataToSend = data;
>
> webRequest.BeginGetRequestStream(new AsyncCallback(ReadCallbackRequest), rs);
>
> IAsyncResult r = (IAsyncResult)webRequest.BeginGetResponse(new AsyncCallback(RespCallback), rs);
>
> ThreadPool.RegisterWaitForSingleObject(r.AsyncWait Handle, new WaitOrTimerCallback(ScanTimeoutCallback), rs, (30 * 1000), true);
>
> allDone.WaitOne();
> rs.ResponseStream.Close();
>
> }
> }
> catch (WebException ex) { string str = ex.ToString(); }
> catch (Exception ex) { string sr = ex.ToString(); }
> }
>
>
> private static void RespCallback(IAsyncResult ar)
> {
> RequestState rs = (RequestState)ar.AsyncState;
> try
> {
> WebRequest req = rs.Request;
>
> WebResponse resp = req.EndGetResponse(ar);
>
> Stream ResponseStream = resp.GetResponseStream();
>
> rs.ResponseStream = ResponseStream;
> rs.Waitone.Set();
>
> if (rs.Debug)
> {
> IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
> }
> }
> catch (Exception ex)
> {
> rs.Waitone.Set();
> }
> }
>
>
> private static void ReadCallbackRequest(IAsyncResult asynchronousResult)
> {
> RequestState rs = (RequestState)asynchronousResult.AsyncState;
> try
> {
> HttpWebRequest request = rs.Request;
>
> Stream postStream = request.EndGetRequestStream(asynchronousResult);
>
> postStream.Write(rs.DataToSend, 0, rs.DataToSend.Length);
> postStream.Close();
> }
> catch (Exception ex)
> {
> }
> allDone.Set();
>
> }
>
> private static void ScanTimeoutCallback(object state, bool timedOut)
> {
> if (timedOut)
> {
> RequestState reqState = (RequestState)state;
> if (reqState != null)
> reqState.Request.Abort();
> }
> }
>
> public class RequestState
> {
> const int BufferSize = 1024;
> public byte[] DataToSend;
> public StringBuilder RequestData;
> public byte[] BufferRead;
> public HttpWebRequest Request;
> public Stream ResponseStream;
> public ManualResetEvent Waitone;
> public bool Debug;
>
> public Decoder StreamDecode = Encoding.UTF8.GetDecoder();
>
> public RequestState(ManualResetEvent waitHandle,bool debug)
> {
> BufferRead = new byte[BufferSize];
> RequestData = new StringBuilder(String.Empty);
> Request = null;
> ResponseStream = null;
> Waitone = waitHandle;
> Debug = debug;
> }
> }
> }
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: async mode dedicated versus async mode interactive!! Pink_Floyd Cisco 4 06-16-2006 12:16 AM
Async webservice call in async webpage (.Net 2.0) does not return Steven ASP .Net Web Services 0 11-30-2005 01:06 AM
Call for Papers: ASYNC-2005 (New York City) Montek Singh VHDL 0 07-29-2004 05:23 PM
Using a BlockRam in an async FIFO for bus width conversion ? Arnaud VHDL 3 06-28-2004 04:33 PM
[Q] : async event counter dong seok huh VHDL 6 10-06-2003 11:04 AM



Advertisments