Created
May 8, 2012 20:51
-
-
Save micahasmith/2639171 to your computer and use it in GitHub Desktop.
asyncronous httphandler web request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RequestState | |
{ | |
public WebRequest Wr { get; set; } | |
public HttpContext Cxt { get; set; } | |
public static string ReadStream(Stream i) | |
{ | |
using (StreamReader sr = new StreamReader(i)) | |
{ | |
return sr.ReadToEnd(); | |
} | |
} | |
} | |
public class async_webrequest_test : IHttpAsyncHandler | |
{ | |
public void ProcessRequest(HttpContext context) | |
{ | |
throw new NotImplementedException(); | |
} | |
public bool IsReusable | |
{ | |
get | |
{ | |
return true; | |
} | |
} | |
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) | |
{ | |
var r = new RequestState() | |
{ | |
Wr = WebRequest.Create("http://www.google.com"), | |
Cxt = context | |
}; | |
r.Wr.Method = "GET"; | |
return r.Wr.BeginGetResponse(cb, r); | |
} | |
public void EndProcessRequest(IAsyncResult result) | |
{ | |
var r = (RequestState)result.AsyncState; | |
var s=r.Wr.EndGetResponse(result); | |
r.Cxt.Response.ContentType = "text/plain"; | |
r.Cxt.Response.Write(string.Format("Hello World async {0}",RequestState.ReadStream( s.GetResponseStream()))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
retested with isreusable=true