有一个示例请求:
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'
我不明白为什么在这里未经授权?以及如何解决问题?