C#解決“因為算法不同,客戶端和服務器無法通信”的問題
故障現象
實現微信退款功能,我們需要在微信支付商戶后臺申請安全證書,并調用退款API URL。在調試過程中為增添返回調試信息屬性,重新對.net FrameWorkd 類庫進行編譯并部署,調試一切正常,但再次覆蓋的時候,調用顯示為 “ 因為算法不同,客戶端和服務器無法通信。” ,系統(tǒng)返回錯誤:

類似調用如下代碼:
string cert = @"D:\wxpay\apiclient_cert.p12"; string password = "14302"; string post_data = getRefundOrderXml(refundorder, key); string request_data = PostXmlAndCertToUrl(RefundOrderUrl, post_data,cert,password);
問題出在 PostXmlAndCertToUrl 調用上,cert 為申請證書的存放位置,passwrd 為證書密碼。
開發(fā)運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.7.2
開發(fā)工具:VS2019 C#
解決
System.Net.ServicePointManager.SecurityProtocol 屬性可選擇安全套接字層 (SSL) 或傳輸層安全 (TLS) 協(xié)議的版本,可能是由于協(xié)議版本不匹配造成的此原因,通過在Page_Load 服務器事件添加如下語句,問題解決:
void Page_Load(Object sender, EventArgs e)
{
System.Net.ServicePointManager.SecurityProtocol =
System.Net.SecurityProtocolType.Tls |
System.Net.SecurityProtocolType.Tls11 |
System.Net.SecurityProtocolType.Tls12;
} 實現攜帶證書的 API URL調用
PostXmlAndCertToUrl 實現了攜帶安全證書訪問 API 的能力,說明見下表:
| 序號 | 參數名 | 類型 | 說明 |
|---|---|---|---|
| 1 | url | string | 要訪問的 API URL 地址 |
| 2 | post_data | string | 要 POST 的指定規(guī)則內容 |
| 3 | cert | string | API 安全證書存放存儲的全路徑地址 |
| 4 | password | string | 證書密碼 |
實現代碼如下:
public string PostXmlAndCertToUrl(string url, string postData,string cert,string password)
{
string resp = string.Empty;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
System.Security.Cryptography.X509Certificates.X509Certificate2 cer = new System.Security.Cryptography.X509Certificates.X509Certificate2(cert, password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.PersistKeySet | System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.MachineKeySet);
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
webrequest.ClientCertificates.Add(cer);
webrequest.Method = "post";
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.ContentLength = postData.Length;
HttpWebResponse response = null;
try
{
StreamWriter swRequestWriter = new StreamWriter(webrequest.GetRequestStream());
swRequestWriter.Write(postData);
if (swRequestWriter != null)
swRequestWriter.Close();
response = (HttpWebResponse)webrequest.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
resp = reader.ReadToEnd();
}
}
catch (Exception exp)
{
throw exp;
}
finally
{
if (response != null)
response.Close();
}
return resp;
}
private static bool CheckValidationResult(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors)
{
if (errors == System.Net.Security.SslPolicyErrors.None)
return true;
return false;
}到此這篇關于C#解決“因為算法不同,客戶端和服務器無法通信”的問題的文章就介紹到這了,更多相關C#客戶端和服務器無法通信內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C# 控件屬性和InitializeComponent()關系案例詳解
這篇文章主要介紹了C# 控件屬性和InitializeComponent()關系案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

