public class ICBCUtil
{
public static string CheckOrder(
string apiUrl,
string strOrderNum,
string strTranDate,
string strShopCode,
string strShopAccount,
out string errInfo)
{
errInfo = string.Empty;
//string api_url = "https://工行接口服务器地址/servlet/ICBCINBSEBusinessServlet";
string api_url = "https://corporbank.icbc.com.cn/servlet/ICBCINBSEBusinessServlet";
string post_params = "APIName=EAPI&APIVersion=001.001.002.001&MerReqData=";
string cert_path = HttpContext.Current.Server.MapPath("~\\bin\\ca.cer");
string cert_password = "********";
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);
return PostDataBySSL(post_data, apiUrl, 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();
//Encoding encoding = Encoding.Default;
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;
}
}
PostDataBySSL这个方法SSL连接有没有问题,执行后服务器返回有异常信息
远程服务器返回错误: (403) 已禁止。
不知道是不是证书问题,还是证书的权限不够,
请知道的大牛指点指点。