經過證書請求Https站點

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

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

如今來看程序怎麼實現。web

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

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

271755537353009

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

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

調用url

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

271802486575203

二、調用code

<!-- 平安銀行證書路徑-->
    <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到指定的地址上。orm

相關文章
相關標籤/搜索