2011-09-24 127 views
2

我从以下http处理程序中得到“byte [] param = [...]”的错误。其他ashx文件正在工作。告诉我,如果你需要更多的信息.​​..ashx错误http处理程序2


请求不可用在这方面

说明:在当前Web请求的执行过程中发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。

异常详细信息:System.Web.HttpException:请求不可用在这方面


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; 

回答

3

为什么会有对System.Web.UI.Page的Request对象的调用?它没有一个,因为没有与请求相关联。

您的代码:

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); 

难道不应该阅读

string strRequest; 
StreamReader reader = new StreamReader(context.Request.InputStream); 
strRequest = reader.ReadToEnd(); 

如果你想要做的就是传入请求的原始字符串。