【问题标题】:ashx error http handler 2ashx 错误 http 处理程序 2
【发布时间】:2011-09-24 16:42:23
【问题描述】:

我在“byte[] param = [...]”处收到来自以下 http 处理程序的错误。其他 ashx 文件正在运行。如果您需要更多信息,请告诉我...


请求在此上下文中不可用

说明:在执行当前 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;

【问题讨论】:

    标签: c# asp.net ashx


    【解决方案1】:

    为什么会调用 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();
    

    如果您只想获取传入请求的原始字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      相关资源
      最近更新 更多