C#使用HttpClient调用api时未经授权

时间:2016-03-08 02:48:18

标签: c# api

有一个示例请求:

curl http://localhost:8080/auth/info \
-H 'Content-type: application/json' \
-H 'X-AUTH-TOKEN: token' \
-X GET

我尝试用C#编写:

string api = "http://localhost:8080/"
HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri(api);
hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "auth/info");
request.Headers.Add("x-auth-token", "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ");
hc.SendAsync(request).ContinueWith(responseTask =>
{
    Console.WriteLine("Response: {0}", responseTask.Result);
});
    Console.ReadLine();

然后responseTask.Result返回:

StatusCode: 401, ReasonPhrase: 'Unauthorized'   

我不明白为什么在这里未经授权?以及如何解决问题?

0 个答案:

没有答案