I use this code from C # to call a php web service that is protected by user / password and certificate:
string wsurl = ConfigurationManager.AppSettings["Url"];
string wsuser = ConfigurationManager.AppSettings["User"];
string wspass = ConfigurationManager.AppSettings["Pass"];
string url = string.Format(wsurl, param1, param2);
System.Net.CredentialCache oCredentialCache = new System.Net.CredentialCache();
oCredentialCache.Add(new System.Uri(url), "Basic",
new System.Net.NetworkCredential(wsuser, wspass));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Timeout = TIMEOUT;
req.Credentials = oCredentialCache;
req.Method = "GET";
AttachCert(req);
WebResponse resp = req.GetResponse();
This works as expected when it starts from my devel machine, but when we upload it to the server, I get a Timeout error when calling GetResponse ().
The AttachCert () call is where I attach the certificate to the request, and if I comment on this line, I do not get a timeout, but the correct error is that the call cannot be completed due to a missing certificate. This is the AttachCert code:
public static void AttachCert(HttpWebRequest Request)
{
string certpath = ConfigurationManager.AppSettings["CertPath"];
X509Certificate Cert = X509Certificate.CreateFromCertFile(certpath);
ServicePointManager.CertificatePolicy = new CertPolicy();
Request.ClientCertificates.Add(Cert);
}
Any idea why this will work on my machine but not on the server? We tried to remove the certificate in the web service and it worked as expected. So, obviously, there is something strange in this call, but I can’t understand that.
Thanks Vicenç