我正在使用TransferMode.Streamed HttpSelfHostConfiguration exe中的WebApi编写代理。
当我使用fiddler发布到我的ApiController时,由于某种原因,我无法读取Request.Content -即使我有POSTed数据,它也会返回"“
public class ApiProxyController : ApiController
{
public Task<HttpResponseMessage> Post(string path)
{
return Request.Content.ReadAsStringAsync().ContinueWith(s =>
{
var content = new StringContent(s.Result); //s.Result is ""
CopyHeaders(Request.Content.Headers, content.Headers);
return Proxy(path, content);
}).Unwrap();
}
private Task<HttpResponseMessage> Proxy(string path, HttpContent content)
{
...
}
}这是我的web请求
POST http://localhost:3001/api/values HTTP/1.1
Host: localhost:3001
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Content-Type: application/json
Content-Length: 26
{ "text":"dfsadfsadfsadf"}我做错了什么?为什么s.Result返回的是空字符串而不是原始json?
发布于 2020-01-01 04:07:40
下面的答案展示了如何从WebAPI中读取POST数据:
string postData;
using (var stream = await request.Content.ReadAsStreamAsync())
{
stream.Seek(0, SeekOrigin.Begin);
using (var sr = new StreamReader(stream))
{
postData = await sr.ReadToEndAsync();
}
}https://stackoverflow.com/questions/10127803
复制相似问题