2011-06-22 26 views
8

我有以下代碼:缺少基本的HTTP身份驗證入口

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 

無認證條目。我錯過了什麼?

回答

34

嘗試的老式方法:

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

IIRC Web客戶端不發送的授權請求報頭,直到它到達一個挑戰來自服務器401