2013-04-21 64 views
1

我使用ServiceDescriptionImporter從wsdl動態生成一個Web服務客戶端(運行時)。將HTTP頭添加到HttpSoapClientProtocol

一切工作很好,直到我嘗試調用需要身份驗證(基本)的服務。我無法找到任何方法將基本身份驗證標頭添加到由我生成的客戶端發送的請求中。

我已經嘗試了以下,但它不起作用。我仍然得到一個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)?

/Andreas

回答

0

問題解決了!我幾乎是正確的。只需要將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;