【发布时间】:2012-04-09 17:38:57
【问题描述】:
我正在尝试将HttpClient 用于需要基本 HTTP 身份验证的第三方服务。我正在使用AuthenticationHeaderValue。到目前为止,这是我想出的:
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 操作工作正常,但我没有取回我期望的数据。经过反复试验,最终使用 Fiddler 嗅探原始流量,我发现授权标头没有被发送。
我见过this,但我认为我已将身份验证方案指定为AuthenticationHeaderValue 构造函数的一部分。
有什么我错过的吗?
【问题讨论】:
标签: c# .net-4.5 wcf-web-api dotnet-httpclient