[英]HTTP status code 403 received when trying to retrieve response from GetAsync
我正在嘗試使用GetAsync
從API獲取一些信息,但是即使在添加帶有為我提供的令牌的OAuth授權標頭后,我仍得到403的狀態代碼。 還有什么我需要做的,還是令牌不好?
class TestAPI
{
static void Main()
{
var client = new HttpClient();
client.BaseAddress = new Uri(BASE_ADDRESS_FOR_TESTING);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", TOKEN);
HttpResponseMessage response = await client.GetAsync("WebApi/CaseLogs/v10/Search?DateFrom=04-05-2012");
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
}
else
{
Console.WriteLine((int) response.StatusCode); // prints "403"
}
}
}
您的HttpClient在我看來還不錯。 我認為您的令牌不好。
403錯誤也支持該理論。
403 FORBIDDEN服務器理解了請求,但拒絕授權。
希望公開為什么禁止請求的服務器可以在響應有效負載(如果有)中描述該原因。 https://httpstatuses.com/403
您還應該打印響應消息。
using ( HttpResponseMessage response = await client.GetAsync("WebApi/CaseLogs/v10/Search?DateFrom=04-05-2012"))
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
Console.WriteLine(result);
}
嘗試使用此代碼,以查看您是否在響應內容中獲得了更多信息。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.