2010-09-14 66 views
0

嗨這工作時發佈到http地址,但發佈到HTTPS地址發佈失敗,無法建立信任關係!c#.net4 webRequest通過SSL不起作用

我需要做些什麼或這是一個服務器錯誤!?

private static string HttpPost (string uri, string parameters) 
{ 
    //return "ok"; 
    // parameters: name1=value1&name2=value2  
    try 
    { 
     WebRequest webRequest = WebRequest.Create(uri); 

     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     webRequest.Method = "POST"; 
     byte[] bytes = Encoding.ASCII.GetBytes(parameters); 
     Stream os = null; 
     try 
     { // send the Post 
      webRequest.ContentLength = bytes.Length; //Count bytes to send 
      os = webRequest.GetRequestStream(); 
      os.Write(bytes, 0, bytes.Length);   //Send it 
     } 
     catch (WebException ex) 
     { 
      MessageBox.Show(ex.Message, "HttpPost: Request error", 
       MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     finally 
     { 
      if (os != null) 
      { 
       os.Close(); 
      } 
     } 

     try 
     { // get the response 
      WebResponse webResponse = webRequest.GetResponse(); 
      if (webResponse == null) 
      { return null; } 
      StreamReader sr = new StreamReader(webResponse.GetResponseStream()); 
      return sr.ReadToEnd().Trim(); 
     } 
     catch (WebException ex) 
     { 
      MessageBox.Show(ex.Message, "HttpPost: Response error", 
       MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 
    catch 
    { 

    } 
    return null; 
} // end HttpPost 
+1

你說它失敗了......你有什麼異常?如果您在瀏覽器中訪問https站點,它是否正常工作? – Nik 2010-09-14 20:04:10

+0

BTW:您使用HttpWebRequest而不是[WebClient](http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx)[.UploadValues](http:// msdn .microsoft.com/EN-US /庫/ 9w7b4fz7.aspx)? – dtb 2010-09-14 20:05:52

+0

嗨例外是: – Adrian 2010-09-14 20:14:23

回答

1

SSL證書已過期或無效。在服務器上部署新的有效SSL證書。

1

或者明確地信任證書,如果這是正確的事情。

+0

相同的問題,我發現這個代碼的作品,但不確定的影響或如果它將在退出後駐留!? – Adrian 2010-09-14 21:40:29

+0

ServicePointManager.ServerCertificateValidationCallback = 新的RemoteCertificateValidationCallback(委託( object sender2,X509Certificate certificate, X509Chain chain,SslPolicyErrors sslPolicyErrors) { return true; }); – Adrian 2010-09-14 21:40:50