HTTP请求标头中缺少基本HTTP身份验证条目

时间:2011-06-22 12:58:14

标签: c# webclient

我有以下代码:

WebClient client = new WebClient();
String un = "Username";
String pw = "Password";
client.Credentials =  new System.Net.NetworkCredential(un,pw);
client.DownloadFileCompleted +=
            new AsyncCompletedEventHandler(downloadFileCompleted);
client.DownloadFileAsync(new Uri(url), Config.LocalDir + @"\data\supportData.xml");

在服务器上使用NetworkMonitor我收到以下内容:

Http: Request, GET /audiClave/REST/en/actions 
Command: GET
URI: /audiClave/REST/en/actions
ProtocolVersion: HTTP/1.1
Host:  210.xxx.xxx.xxx:8080
Connection:  Keep-Alive
HeaderEnd: CRLF

无验证条目。我错过了什么?

1 个答案:

答案 0 :(得分:35)

尝试老式的方式:

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(un + ":" + pw));
client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;

IIRC WebClient在从401服务器获得质询之前不会发送授权请求标头。