2013-04-21 91 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;