c# - ashx 错误 http 处理程序 2

标签 c# asp.net ashx

我从以下 http 处理程序中收到“byte[] param = [...]”错误。其他 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;

最佳答案

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

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

关于c# - ashx 错误 http 处理程序 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7540363/

相关文章:

c# - 登录用户名和文本框文本未保存在数据库中

c# - ASHX 文件下载损坏的文件

c# - 我是否以错误的方式使用处理程序?

jquery - .ashx 处理程序不在服务器上工作,仅在本地工作

c# - ASP.NET中页面重定向引发的异常

c# - EWS 托管 API : Reply to a message while adding an internet header

c# - ListView C# (.Net 3.5) 中的备用颜色?

c# - 向数据库中插入一行

javascript - 使用 javascript 将值从下拉列表(绑定(bind)到数据库)传递到文本框

c# - 将代理 PAC 与 EWS API 结合使用