通過證書請求Https站點


前幾天在做與平安銀行對接接口,主要是給平安銀行推送用戶數據(申請貸款的用戶),平安銀行提供的是https的地址,請求https地址的時候還要發送證書,剛接到這個任務的時候一頭霧水,百度上各種所搜,最后還是給解決了。

幸好前幾天在博客園里看到一篇文章,給了我很大幫助,地址:http://www.cnblogs.com/caiwenz/p/3913461.html

現在來看程序怎么實現。

首先看一下證書,下圖是平安銀行接口人給發送的證書,里面的證書有java使用的,有PHP使用的,也有.NET使用,當我打電話向平安銀行接口人咨詢.NET需要用到那個證書時,對方的回答他也不知道,然后只能去百度了。

其中紅色框圈住的是.NET需要的證書

程序實現

public class HttpHelper
    {
       /// <summary>
       /// 證書路徑
       /// </summary>
       public string CertificateFilePath { get; set; }
       /// <summary>
       /// 證書密碼
       /// </summary>
       public string CertificateFilePwd { get; set; }

       public HttpHelper()
       {
           //ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;//驗證服務器證書回調自動驗證
       }
       /// <summary>
       /// 發送POST請求
       /// </summary>
       /// <param name="url">請求的地址</param>
       /// <param name="Content">請求的內容</param>
       /// <param name="isLoadCert">是否加載證書</param>
       /// <returns></returns>
       public String Post(String url, String Content, bool isLoadCert)
       {
           string html = "";
           HttpWebRequest webReqst = (HttpWebRequest)WebRequest.Create(url);
           if (isLoadCert)
           {
               //創建證書
               X509Certificate2 cert = CreateX509Certificate2();
               //添加證書認證
               webReqst.ClientCertificates.Add(cert);
           }
           webReqst.Method = "POST";
           webReqst.KeepAlive = true;
           webReqst.ContentType = "application/x-www-form-urlencoded";
           try
           {
               byte[] data = Encoding.Default.GetBytes(Content);
               webReqst.ContentLength = data.Length;
               Stream stream = webReqst.GetRequestStream();
                   stream.Write(data, 0, data.Length);
                   HttpWebResponse webResponse = (HttpWebResponse)webReqst.GetResponse();
                   if (webResponse.StatusCode == HttpStatusCode.OK && webResponse.ContentLength < 1024 * 1024)
                   {
                       StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.Default);
                       html = reader.ReadToEnd();
                   }
           }
           catch(Exception ex)
           {
               throw ex;
           }

           return html;
       }

        /// <summary>
        /// 創建證書
        /// </summary>
        /// <returns>X509Certificate2對象</returns>
        public X509Certificate2 CreateX509Certificate2()
        {
            X509Certificate2 cert = null;
            try
            {
                cert = new X509Certificate2(CertificateFilePath, CertificateFilePwd);
                ServicePointManager.ServerCertificateValidationCallback =
                    new RemoteCertificateValidationCallback(ServerCertificateValidationCallback);
            }
            catch (Exception ex)
            {
                throw ex;  
            }
            return cert;
        }

       /// <summary>
       /// 驗證證書的回調函數
       /// </summary>
       /// <param name="obj"></param>
       /// <param name="cer"></param>
       /// <param name="chain"></param>
       /// <param name="error"></param>
       /// <returns></returns>
        private bool ServerCertificateValidationCallback(object obj, X509Certificate cer, X509Chain chain, System.Net.Security.SslPolicyErrors error)
        {
            return true;
        }
    }

程序比較簡單了,主要是請求證書,以前沒搞過。

調用

1、把證書放在電腦的一個盤中,記錄.pfx證書的路徑,還需要知道證書的密碼

2、調用

   <!-- 平安銀行證書路徑-->
    <add key="CertificateFilePath" value="D:\證書\證書\store.pfx" />
    <!-- 平安銀行證書密碼-->
    <add key="CertificateFilePwd" value="XXXX" />
    <!--平安銀行請求的地址-->
    <add key="PingAnUrl" value="https://XXXX7" />
 HttpHelper helper = new HttpHelper();
                    helper.CertificateFilePath = WindowsServiceCommon.GetConfigSetting("CertificateFilePath");  //ConfigurationManager.AppSettings["CertificateFilePath"].ToString();
                    helper.CertificateFilePwd = WindowsServiceCommon.GetConfigSetting("CertificateFilePwd");  //ConfigurationManager.AppSettings["CertificateFilePwd"].ToString();
                    var html = helper.Post(WindowsServiceCommon.GetConfigSetting("PingAnUrl"), XmlContent, true);

 

 

 這樣就成功的吧數據Post到指定的地址上。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM