C# 在 HTTP 处理程序中接收 HTTP POST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16996713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:18:19  来源:igfitidea点击:

Receiving a HTTP POST in HTTP Handler?

c#asp.netposthttp-posthttphandler

提问by Sandy

I need to listen and process a HTTP POST string in a HTTP handler.

我需要在 HTTP 处理程序中监听和处理 HTTP POST 字符串。

Below is the code for posting the string to handler -

以下是将字符串发布到处理程序的代码 -

string test = "charset = UTF-8 & param1 = val1 & param2 = val2 & param3 = val3 & param4 = val4;
byte[] data = Encoding.UTF8.GetBytes(test);
PostData("http://localhost:53117/Handler.ashx", data);

What I tried in Handler is -

我在 Handler 中尝试的是 -

    public void ProcessRequest(HttpContext context)
    {
        var value1 = context.Request["param1"];
    }

But its null. How can I listen and get the parameter values in Handler?

但它的空值。如何在 Handler 中监听和获取参数值?

采纳答案by Darin Dimitrov

You don't seem to be using any of the standard request encodings, instead you seem to be reinventing some custom protocol, so you cannot rely on the server ASP.NET to be able to parse this request. You will have to read the values directly from the InputStream:

您似乎没有使用任何标准请求编码,而是您似乎正在重新发明一些自定义协议,因此您不能依赖服务器 ASP.NET 来解析此请求。您必须直接从 InputStream 读取值:

public void ProcessRequest(HttpContext context)
{
    using (var reader = new StreamReader(context.Request.InputStream))
    {
        // This will equal to "charset = UTF-8 & param1 = val1 & param2 = val2 & param3 = val3 & param4 = val4"
        string values = reader.ReadToEnd();
    }
}

If on the other hand you use some standard request encoding such as application/x-www-form-urlencodedyou will be able to read the parameters as usual.

另一方面,如果您使用一些标准的请求编码,例如application/x-www-form-urlencoded您将能够像往常一样读取参数。

Here's how such a request payload might look like:

以下是此类请求有效负载的外观:

POST /Handler.ashx HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 47
Connection: close

param1=val1&param2=val2&param3=val3&param4=val4

In order to send such a request you could use a WebClient:

为了发送这样的请求,您可以使用WebClient

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "param1", "value1" },
        { "param2", "value2" },
        { "param3", "value3" },
        { "param4", "value4" },
    };
    byte[] result = client.UploadValues(values);
}

Now on the server you can read the values like that:

现在在服务器上,您可以读取这样的值:

public void ProcessRequest(HttpContext context)
{
    var value1 = context.Request["param1"];
    var value2 = context.Request["param2"];
    ...
}

回答by Ratna

Change

改变

    var value1 = context.Request["param1"];

to

    var value1 = context.Request.Form["param1"];

回答by gafda

It's actually :

它实际上是:

    context.Request.Params["param1"];