ashx error http handler 2
I'm getting the an error at "byte[] param = [...]" from the following http handler. Other ashx files are working. Tell me if you need more info...
Request is not available in this context
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Request is not available in this context
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
System.Web.UI.Page pa = new System.Web.UI.Page();
//HERE>HERE>HERE&g开发者_StackOverflow中文版t;HERE>
byte[] param = pa.Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
Why is there a call to System.Web.UI.Page's Request object? It won't have one since one hasn't been associated with the request.
Your code:
System.Web.UI.Page pa = new System.Web.UI.Page();
//HERE>HERE>HERE>HERE>
byte[] param = pa.Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
Shouldn't it read
string strRequest;
StreamReader reader = new StreamReader(context.Request.InputStream);
strRequest = reader.ReadToEnd();
If all you want to do is get the raw string of the incoming request.
精彩评论