【问题标题】:Missing Basic HTTP authentication entry in HTTP request headerHTTP 请求标头中缺少基本 HTTP 身份验证条目
【发布时间】:2011-06-22 12:58:14
【问题描述】:

我有以下代码:

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

没有身份验证条目。我错过了什么?

【问题讨论】:

    标签: c# webclient


    【解决方案1】:

    尝试老式的方法:

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

    IIRC WebClient 在收到来自服务器的 401 质询之前不会发送授权请求标头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 2015-05-05
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      相关资源
      最近更新 更多