C# https證書通訊Post/Get(解決作ssl通道時遇到「請求被停止: 未能建立 SSL/TLS 安全通道」問題)

 1         public static string HttpPost(string url, string param = null)  2  {  3  HttpWebRequest request;  4 
 5             //若是是發送HTTPS請求 
 6             if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))  7  {  8                 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;   //協議按需選擇,
 9                 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); 10                 request = WebRequest.Create(url) as HttpWebRequest; 11                 request.ProtocolVersion = HttpVersion.Version10; 12 
13  } 14             else
15  { 16                 request = WebRequest.Create(url) as HttpWebRequest; 17  } 18 
19             request.Method = "POST"; 20             request.ContentType = "application/json"; 21             request.Accept = "*/*"; 22             request.Timeout = 15000; 23             request.AllowAutoRedirect = false; 24             //查找咱們導入的證書
25             X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); 26  certStore.Open(OpenFlags.ReadOnly); 27             var aa = certStore.Certificates; 28             X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindBySubjectName, "www.xxx.com", false); 29             request.ClientCertificates.Add(certCollection[0]); 30 
31 
32             StreamWriter requestStream = null; 33             WebResponse response = null; 34             string responseStr = null; 35 
36             try
37  { 38                 requestStream = new StreamWriter(request.GetRequestStream()); 39  requestStream.Write(param); 40  requestStream.Close(); 41 
42                 response = request.GetResponse(); 43                 if (response != null) 44  { 45                     StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 46                     responseStr = reader.ReadToEnd(); 47  reader.Close(); 48  } 49  } 50             catch (Exception) 51  { 52                 throw; 53  } 54             finally
55  { 56                 request = null; 57                 requestStream = null; 58                 response = null; 59  } 60 
61             return responseStr; 62  } 63 
64 
65         private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 66  { 67             return true; //老是接受 
68         }
View Code

第一步導入證書:json

a、運行輸入「mmc」app

 b、在控制檯中點擊「文件」——>「添加/刪除管理單元」,在彈出框中依次選擇「證書」-「添加」-「」-「肯定」ide

上圖點擊「添加」彈出框選擇「計算機帳戶」,一直下一步到完成。工具

 

c、證書導入,「證書」右鍵 選擇「全部任務」,點擊「導入「。網站

 

 d、證書受權url

 一、工具下載 安裝winhttpcertcfg.msi 下載地址 https://www.microsoft.com/en-us/download/details.aspx?id=19801。spa

 二、到winhttpcertcfg安裝目錄運行以下命令提高帳戶訪問權限:WinHttpCertCfg.exe -g -c LOCAL_MACHINE\MY -s "證書名" -a  "NETWORK SERVICE"。code

      說明:-s  證書名(如:www.xxxx.com) ;-a 受權用戶blog

 三、若是你的IIS是7.0,須要設置網站應用池標識(如圖)。string

 

四、重啓IIS

相關文章
相關標籤/搜索