将HTTP标头添加到HttpSoapClientProtocol

时间:2013-04-21 22:20:51

标签: .net http soap header client

我正在使用ServiceDescriptionImporter从wsdl动态生成(运行时)Web服务客户端。

在尝试调用需要身份验证的服务(基本)之前,一切正常。我找不到任何方法将基本的auth标头添加到我生成的客户端发送的请求中。

我尝试了以下但是它不起作用。我仍然得到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)?

/安德烈亚斯

1 个答案:

答案 0 :(得分: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;