Calling webservice in C# using SSL - '(401) Unauthorized'

Chris B picture Chris B · Apr 28, 2009 · Viewed 27.3k times · Source

I'm calling a method on a web service from behind a proxy server using the following code:

myWebService.TestWebService webservice = new myWebService.TestWebService();
webservice.Url = "http://test.com/webservice?wsdl";

WebProxy proxy = new WebProxy("1.2.3.4", 8080);
proxy.Credentials = new NetworkCredential("username", "password");
webservice.Proxy = proxy;

string response = webservice.TestWebMethod();

This works fine when using HTTP, I get the response I'm expecting in the 'response' string. However - if I change the URL to HTTPS then I get a (401) Unauthorized response.

If I put the URL into my browser it works fine using HTTP or HTTPS.

I've added code to handle the SSL certificate validation by creating a System.Net.ServicePointManager.ServerCertificateValidationCallback delegate but the code never gets this far. The request is rejected before it validates the certificate or so it seems.

Any help is really appreciated...

Answer

stevehipwell picture stevehipwell · Apr 28, 2009

Do you need credentials to navigate to the SSL url?
If so you need the web service credentials set.

Have you tried adding a web reference in Visual Studio using the SSL url?
If you can't add web reference through Visual Studio then the code is not going to work either.

Can you make sure that the last thing that you set is the proxy (e.g. change the url before you set the proxy)?
There is a small chance that the proxy could be lost, this should be the very last thing to try