Pho*_*fay 6 httpwebrequest asp.net-core-mvc asp.net-core asp.net-core-webapi asp.net-core-2.1
我正在尝试从 HttpRequest.Body 读取流数据,但我得到的是空字符串。请求从 .net 项目发送到这里
HttpWebRequest request = null;
Uri uri = new Uri(**Endpoint**);
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(message);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.UseDefaultCredentials = true;
using (Stream writeStream = request.GetRequestStream()) {
writeStream.Write(bytes, 0, bytes.Length);
}
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
return true;
} else {
return false;
}
} catch {
lock (endpointLock) {
_pushHttpEndpoint = null;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
请求在此处发送。这是 .net core 2.1 应用程序。我正在尝试读取请求正文中的数据,但返回空
[HttpPost]
public string Post()
{
var bodyStr = "";
var req = HttpContext.Request;
req.EnableRewind();
using (StreamReader reader
= new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
{
bodyStr = reader.ReadToEnd();
}
req.Body.Seek(0, SeekOrigin.Begin);
//do other stuff
return bodyStr;
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题。我们处于无法更改 .net 解决方案代码的位置。任何更改都应在 .net 核心解决方案方面完成。我们正在尝试使用新的 api 来代替现有的 Endpoint。:(
kjw*_*mme -3
您应该使用 [FromBody] 属性。
将会是这样的:
[HttpPost]
public string Post([FromBody] object body) {
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2222 次 |
| 最近记录: |