C#版ssl链接(工行查询接口的实现)

 

using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;


Public Class ICBCUtil
{
     public static string CheckOrder(string strOrderNum, string strTranDate, string strShopCode, string strShopAccount, out string errInfo)
     {

        errInfo = string.Empty;
        string api_url = "https://工行接口服务器地址/servlet/ICBCINBSEBusinessServlet";
        string post_params = "APIName=EAPI&APIVersion=001.001.002.001&MerReqData=";
       string cert_path =HttpContext.Current.Server.MapPath("~\\bin\\xxxx.pfx");
        string cert_password = "xxxx";

        StringBuilder sb = new StringBuilder();
       sb.Append("<?xml  version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?><ICBCAPI><in><orderNum>");
        sb.Append(strOrderNum);
        sb.Append("</orderNum><tranDate>");
        sb.Append(strTranDate);
        sb.Append("</tranDate><ShopCode>");
        sb.Append(strShopCode);
        sb.Append("</ShopCode><ShopAccount>");
        sb.Append(strShopAccount);
        sb.Append("</ShopAccount></in></ICBCAPI>");

        string post_data = post_params + sb.ToString();

        return PostDataBySSL(post_data, api_url, cert_path, cert_password,out errInfo);

     }




     private static string PostDataBySSL(string post_data, string url, string cert_path, string cert_password,out string errInfo)
      {
         errInfo=string.Empty;

         try
         {
            ASCIIEncoding encoding = new ASCIIEncoding();
             byte[] data = encoding.GetBytes(post_data);

             if (cert_path != string.Empty)
                 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);


             WebRequest webRequest = WebRequest.Create(url);
             HttpWebRequest httpRequest = webRequest as HttpWebRequest;

             if (cert_path.ToLower().EndsWith(".cer"))
                 httpRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile(cert_path));
             else
                 httpRequest.ClientCertificates.Add(new X509Certificate2(cert_path, cert_password));
             httpRequest.KeepAlive = true;
             httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
             httpRequest.Method = "POST";

             httpRequest.ContentLength = data.Length;
             Stream requestStream = httpRequest.GetRequestStream();
             requestStream.Write(data, 0, data.Length);
             requestStream.Close();

             Stream responseStream = null;

             responseStream = httpRequest.GetResponse().GetResponseStream();
             string stringResponse = string.Empty;
             if (responseStream != null)
             {
                 using (StreamReader responseReader =
                    new StreamReader(responseStream, Encoding.GetEncoding("GBK")))
               {
                    stringResponse = responseReader.ReadToEnd();
                 }
                responseStream.Close();
             }
            return stringResponse;
       }
        catch (Exception e)
        {
            errInfo = e.Message;
            return string.Empty;
      }



    }


    public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
     {
         return true;
     }
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值