无法读取网页API POST主体数据主体、网页、数据、POST

2023-09-03 12:34:46 作者:嘲笑我的痴心

我想提取一些数据在新Asp.Net的Web API使用的一种要求。我有一个处理程序设置是这样的:

I'm trying to extract some data out of a request in the new Asp.Net Web Api. I have a handler setup like this:

public class MyTestHandler : DelegatingHandler
{
    protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        if (request.Content.IsFormData())
        {
            request.Content.ReadAsStreamAsync().ContinueWith(x => {
                var result = "";
                using (var sr = new StreamReader(x.Result))
                {
                    result = sr.ReadToEnd();
                }
                Console.Write(result);
            });
        }

        return base.SendAsync(request, cancellationToken);
    }
}

这是我的http请求:

This is my http request:

POST http://127.0.0.1/test HTTP/1.1
Connection: Keep-Alive
Content-Length: 29
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Host: 127.0.0.1

my_property=my_value

问题是,无论我如何努力读取 request.Content 的信息,它总是空的。我试过

the problem is that no matter how I try to read the info from request.Content it's always empty. I've tried

request.Content.ReadAsStreamAsync
request.Content.ReadAsFormDataAsync
request.Content.ReadAs<FormDataCollection>

以及

    [HttpGet,HttpPost]
    public string Index([FromBody]string my_property)
    {
        //my_property == null
        return "Test";
    }

如果它工作无。我不能得到的数据带出体外。我主持的内部IIS的Windows 7和使用招提交请求。我究竟做错了什么?

None if it works. I cannot get the data out of the body. I'm hosting inside IIS on Windows 7 and using Fiddler to submit the request. What am I doing wrong?

推荐答案

现在的问题是,随着网络的API身体只能读一次。我有一个HTTP模块的运行被登记请求的所有细节,并正在读通过身体。

The problem is that with the Web Api the body can only be read once. I had an HTTP module running that was logging all the details of the request and was reading through the body.

 
精彩推荐
图片推荐