关于c#:HttpClient身份验证标头未被发送

HttpClient authentication header not getting sent

我正在尝试将HttpClient用于需要基本HTTP身份验证的第三方服务。我用的是AuthenticationHeaderValue。以下是我到目前为止的想法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
HttpRequestMessage<RequestType> request =
    new HttpRequestMessage<RequestType>(
        new RequestType("third-party-vendor-action"),
        MediaTypeHeaderValue.Parse("application/xml"));
request.Headers.Authorization = new AuthenticationHeaderValue(
   "Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
        string.Format("{0}:{1}","username","password"))));

var task = client.PostAsync(Uri, request.Content);
ResponseType response = task.ContinueWith(
    t =>
    {
        return t.Result.Content.ReadAsAsync<ResponseType>();
    }).Unwrap().Result;

看起来post-action工作得很好,但是我无法恢复我期望的数据。通过一些尝试和错误,最终使用fiddler嗅探原始流量,我发现授权头没有被发送。

我见过这个,但我认为我已经得到了作为AuthenticationHeaderValue构造函数一部分指定的身份验证方案。

有什么我错过的吗?


您的代码看起来应该可以工作-我记得在设置授权头时遇到了类似的问题,通过执行headers.add()而不是设置它来解决:

1
request.Headers.Add("Authorization","Basic" + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}","username","password"))));

更新:看起来像是在执行请求时。内容,并不是所有的标题都反映在内容对象中。您可以通过检查request.headers与request.content.headers来查看这一点。您可能想要尝试的一件事是使用sendasync而不是postasync。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
HttpRequestMessage<RequestType> request =
     new HttpRequestMessage<RequestType>(
         new RequestType("third-party-vendor-action"),
         MediaTypeHeaderValue.Parse("application/xml"));

request.Headers.Authorization =
    new AuthenticationHeaderValue(
       "Basic",
        Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}","username","password"))));

 request.Method = HttpMethod.Post;
 request.RequestUri = Uri;
 var task = client.SendAsync(request);

 ResponseType response = task.ContinueWith(
     t =>
         { return t.Result.Content.ReadAsAsync<ResponseType>(); })
         .Unwrap().Result;


这也可以,您不必处理base64字符串转换:

1
2
3
4
var handler = new HttpClientHandler();
handler.Credentials = new System.Net.NetworkCredential("username","password");
var client = new HttpClient(handler);
...


尝试在客户端上设置头:

1
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", userName, password))));

这对我有用。


实际上,您的问题在于PostAsync--您应该使用SendAsync。在您的代码中,client.PostAsync(Uri, request.Content);只发送请求消息头不包含的内容。正确的方法是:

1
2
3
4
5
6
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, url)
{
    Content = content
};
message.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
httpClient.SendAsync(message);