欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#解決“因為算法不同,客戶端和服務(wù)器無法通信”的問題

 更新時間:2024年12月24日 09:36:17   作者:初九之潛龍勿用  
實現(xiàn)微信退款功能,我們需要在微信支付商戶后臺申請安全證書,并調(diào)用退款API URL,在調(diào)試過程中為增添返回調(diào)試信息屬性,調(diào)試一切正常,但再次覆蓋的時候,調(diào)用顯示為 “ 因為算法不同,客戶端和服務(wù)器無法通信,” ,本文介紹了C#解決因為算法不同,客戶端和服務(wù)器無法通信的問題

故障現(xiàn)象

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

類似調(diào)用如下代碼:

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 調(diào)用上,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 服務(wù)器事件添加如下語句,問題解決:

void Page_Load(Object sender, EventArgs e)
{
     System.Net.ServicePointManager.SecurityProtocol = 
System.Net.SecurityProtocolType.Tls | 
System.Net.SecurityProtocolType.Tls11 | 
System.Net.SecurityProtocolType.Tls12;
 
}  

實現(xiàn)攜帶證書的 API URL調(diào)用

PostXmlAndCertToUrl 實現(xiàn)了攜帶安全證書訪問 API 的能力,說明見下表:

序號參數(shù)名類型說明
1urlstring要訪問的 API URL 地址
2post_datastring要 POST 的指定規(guī)則內(nèi)容 
3certstringAPI 安全證書存放存儲的全路徑地址
4passwordstring證書密碼

實現(xiàn)代碼如下:

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

到此這篇關(guān)于C#解決“因為算法不同,客戶端和服務(wù)器無法通信”的問題的文章就介紹到這了,更多相關(guān)C#客戶端和服務(wù)器無法通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論