簡體   English   中英

將HTTP標頭添加到HttpSoapClientProtocol

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM