![](/img/trans.png)
[英]System.Net.Http.HttpClient adding If-Modified-Since header
[英]Adding HTTP header to HttpSoapClientProtocol
我正在使用ServiceDescriptionImporter從wsdl動態(運行時)生成Web服務客戶端。
在嘗試調用需要身份驗證(基本)的服務之前,一切工作都很好。 我找不到任何將基本身份驗證標頭添加到由我生成的客戶端發送的請求中的方法。
我嘗試了以下方法,但不起作用。 我仍然收到401:
var webClient = obj as HttpSoapClientProtocol;
CredentialCache mycache = new CredentialCache();
// Credentials are specified here
mycache.Add(new Uri("http://localhost:9999/MyService"), "Basic", new NetworkCredential("username", "password"));
webClient.Credentials = mycache;
如何將HTTP標頭添加到webClient(HttpWebClientProtocol)?
/安德烈亞斯
問題解決了! 我幾乎是對的。 只需將PreAuthenticate
設置為true。
這是正確的代碼:
var webClient = obj as HttpWebClientProtocol;
NetworkCredential netCredential = new NetworkCredential("username", "password");
Uri uri = new Uri("http://localhost:9999/MyService");
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
webClient.Credentials = credentials;
webClient.PreAuthenticate = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.