英文:
Extract custom message from 401 Unauthorized response
问题
我如何从API 401响应中提取我的自定义消息?
我在API中返回未经授权的消息:
return Unauthorized("我的自定义消息");
在我的客户端Winform应用程序中,我想显示我的自定义消息,但是我从API获取的消息是"响应状态代码不指示成功:401(未经授权)"。
是否可能提取我的消息?
英文:
How do I extract my custom message from the API 401 response?
I return Unauthorized in the API:
return Unauthorized("my custom message");
In my client winform app I want to show my custom message, but the message I get from the API is "Response status code does not indicate success: 401 (Unauthorized)."
Is it possible to extract my message?
答案1
得分: 0
使用类似以下示例的内容:
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
{
using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message: {0}", e.Message);
}
}
你可以从响应体中提取你的消息:
// 从响应体中提取你的消息
string responseBody = response.Content.ReadAsStringAsync().Result;
英文:
Using something like this example: HttpResponseMessage Class
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
{
using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
Yoou can extract your message from the response body:
// Extract your message from the response body
string responseBody = response.Content.ReadAsStringAsync().Result;
答案2
得分: 0
这是我做的事情 -
var response = httpClient.PostAsync(url, hc).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(response.StatusCode + " - " + responseBody);
}
现在我从API获得了自定义错误。
英文:
This is what I did -
var response = httpClient.PostAsync(url, hc).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(response.StatusCode + " - " + responseBody);
}
Now I get my custom error from the API.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论