2010-07-22 36 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); 

我希望它有幫助。