我正在使用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)?
/安德烈亚斯
答案 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;