2010-07-22 115 views
3

我使用的是从C#这样的代码来调用由用户/密码和证书获得了PHP的Web服务:调用.NET从一个PHP Web服务与证书

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); 

//Get the data as an HttpWebResponse object 
WebResponse resp = req.GetResponse(); 

可正常工作时运行从我的开发机器,但是当我们上传到服务器,我得到一个超时错误的GetResponse()的调用。

AttachCert()调用是我将证书附加到请求的地方,如果我对此行发表评论,我不会收到超时,但会有一个正确的错误说明由于缺少证书而导致无法完成调用。 这是AttachCert代码:

public static void AttachCert(HttpWebRequest Request) 
{ 
    // Obtain the certificate. 
    string certpath = ConfigurationManager.AppSettings["CertPath"]; 

    X509Certificate Cert = X509Certificate.CreateFromCertFile(certpath); 
    ServicePointManager.CertificatePolicy = new CertPolicy(); 

    Request.ClientCertificates.Add(Cert); 
} 

任何想法,为什么会在我的机器上,但不是在服务器上运行?我们试图删除Web服务上的证书,并按预期工作。所以这个电话显然有些奇怪,但我无法弄清楚。

感谢 Vicenç

+0

你能跟踪到AttachCert,看看里面哪个呼叫导致超时? – TML 2010-12-31 00:05:02

+0

我在开发和生产之间看到了防火墙的问题。您可以将RDP发布到生产服务器并确认您可以使用IE浏览到PHP Web服务吗? – 2011-01-06 05:40:51

+0

为什么您使用基本身份验证和提供证书? – 2011-01-10 22:52:52

回答

1

尝试注册此回调,看是否有错误。

public static bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors) { 
    // check for errors 
    // just return true to accept any certificate (self signed etc) 
} 

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(MyClass.ValidateRemoteCertificate); 

我希望它有帮助。