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