einUsername
einUsername

Reputation: 1619

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>HERE>
        byte[] param = pa.Request.BinaryRead(HttpContext.Current.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);
        strRequest += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;

Upvotes: 2

Views: 867

Answers (1)

Frazell Thomas
Frazell Thomas

Reputation: 6111

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.

Upvotes: 3

Related Questions