简体   繁体   中英

Adding HTTP header to HttpSoapClientProtocol

I'm generating a web service client dynamically (runtime) from a wsdl using ServiceDescriptionImporter.

Everything works great until I try to invoke a service that requires authentication (Basic). I can't find any way to add basic auth header to the request sent by the client I generated.

I have tried the following but it doesn't work. I still get a 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;

How can I add a HTTP header to the webClient (HttpWebClientProtocol)?

/Andreas

Problem solved! I was almost right. Just needed to set PreAuthenticate to true.

Here is the correct code:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM