简体   繁体   English

使用HttpClient发布并读取HttpResponseMessage状态

[英]Post using HttpClient & Read HttpResponseMessage status

I am posting to an API using HttpClient and getting back the HttpResponseMessage . 我正在使用HttpClient发布到API,并获取HttpResponseMessage I am reading the status code from the reply but I it's always 200 我正在从回复中读取状态代码,但我总是200

Posting: 过帐:

var json = JsonConvert.SerializeObject(loginDto);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var client = new HttpClient();
var response = await client.PostAsync("http://localhost:57770/api/Account/Login", stringContent);

I am replying from API the HttpResponseMessage : 我从API回复HttpResponseMessage

return new HttpResponseMessage(HttpStatusCode.Unauthorized);

But when I read the response , it's always 200 但是当我阅读response ,它总是200

How can I achieve this? 我该如何实现?

Asp.Net Core no longer recognizes HttpResponseMessage as part of the pipeline. Asp.Net Core不再将HttpResponseMessage识别为管道的一部分。 This means it will be treated like any other returned model and serialized as content. 这意味着它将像其他返回的模型一样对待并序列化为内容。 Hence the 200 OK status. 因此为200 OK状态。

The API controller action should return IActionResult derived result. API控制器操作应返回IActionResult派生的结果。

[HttpPost]
public IActionResult SomeAction(...) {

    //...

    return StatusCode((int)HttpStatusCode.Unauthorized); //401

    //...
}

Or just use 或者只是使用

return Unauthorized(); 

which is derived from StatusCodeResult and is used a short hand to replace the code shown above. 它是从StatusCodeResult派生的,并被用作简写形式来替换上面显示的代码。

Reference ControllerBase.Unauthorized . 参考ControllerBase.Unauthorized

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HttpClient和HttpResponseMessage的带有两个字符串的PostAsync - PostAsync with two strings using HttpClient and HttpResponseMessage HttpClient HttpResponseMessage地址/ URI - HttpClient HttpResponseMessage Address / URI 如何使用 HttpClient 从缓存中确定 HttpResponseMessage 是否已完成 - How to determine if an HttpResponseMessage was fulfilled from cache using HttpClient 使用HttpClient时HttpResponseMessage缺少标头,但提琴手找到了 - HttpResponseMessage missing headers while using HttpClient but fiddler finds it 使用 HTTPClient 调用 REST API 时在 httpResponseMessage 中获取 null - Getting null in httpResponseMessage when calling REST API using HTTPClient 状态码不成功时无法读取HttpResponseMessage内容 - Can't read HttpResponseMessage content when the status code is not success HttpClient HttpResponseMessage LastModified文件的日期 - HttpClient HttpResponseMessage LastModified date of file 在Ajax onSuccess中阅读HttpResponseMessage - Read HttpResponseMessage in Ajax onSuccess C#HttpResponseMessage.Content没有将MemoryStream正确返回给HttpClient,而是使用byte []进行返回工作 - c# HttpResponseMessage.Content does not return MemoryStream correctly to HttpClient but using byte[] for return works HttpClient:使用HttpResponseMessage.Content.ReadAsStreamAsync()时限制响应(下载的文件)大小 - HttpClient: limit response (downloaded file) size while using HttpResponseMessage.Content.ReadAsStreamAsync()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM