views:

7711

answers:

2

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...

+1  A: 

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

Stevo3000
I don't need credentials when using a browser, however I just tried adding webservice.Credentials = new NetworkCredential(username, password); and got the same Unathorized response.
@cjb - The credentials for the web service would almost certainly be different to those for the proxy.
Stevo3000
Yes, the proxy is set just before I call the method on the web service. I tried adding a reference to the URL (HTTP and HTTPS) and both times I got a credentails box pop-up for the proxy server, but after entering my details I get a "The proxy name could not be resolved:" and the url of the webservice (not the proxy) which seems a bit wierd...
@cjb - Are you using the same proxy details as your browser? It would apear that you are not as the browser can connect but VS can't. What is the exact message you get?
Stevo3000
I think you might be onto something there - I'm trying to organise access through a different proxy and I'll let you know what happens. I had already checked that the proxy wouldn't be an issue - but have now been told it can play up sometimes...
OK - I have tried it through a different proxy server and it works!I *knew* it wouldn't be a problem with my code ;-)At least I've learned quite a lot by playing around with the certificates/credentials area of .NET. Thanks to Stevo3000 and Rick Hochstetler for their help.
A: 

Here is an example using a client cert (which i'm sure you don't need) but might provide some insight & using credentials to a web service.

WebService.ManageOutboundDelivery oWS = new WebService.ManageOutboundDelivery();

if (My.Settings.HasClientCert == true) {
    X509Certificate2 signedCert = new X509Certificate2(HttpContext.Current.Server.MapPath(My.Settings.ClientCertName), My.Settings.ClientCertPW);
    oWS.ClientCertificates.Add(signedCert);
}

System.Net.CredentialCache oCred = new System.Net.CredentialCache();
Net.NetworkCredential netCred = new Net.NetworkCredential(My.Settings.WebServiceUID, My.Settings.WebServicePW);

oCred.Add(new Uri(oWS.Url), "Basic", netCred);
oWS.Credentials = oCred;

Have you also checked the SSL cert is valid - i'm guessing you would see this when you hit it through the browser but it could be causing a problem trying to hit it programmatically.

Rick Hochstetler