ashx 错误 http 处理程序 2

发布于 2024-12-06 02:40:48 字数 1202 浏览 2 评论 0原文

我从以下 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙味迷妹 2024-12-13 02:40:48

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文