2011-09-24 126 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(); 

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