1

我正在使用 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;

如何向 webClient (HttpWebClientProtocol) 添加 HTTP 标头?

/安德烈亚斯

4

1 回答 1

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;
于 2013-04-22T06:06:47.123 回答