出现的问题:此网站的安全证书有问题。我导入证书也成功了,也添加了可信任站点,为什么还是不行呢?希望开发过的大侠赐教。这是代码部分
前台页面是
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SelectOrderInfo.aspx.cs" Inherits="ICBCBank.SelectOrderInfo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function selectICBC() {
document.order.submit();
}
</script>
</head>
<body onload="selectICBC()">
<form name="order" id="order" action="https://corporbank3.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet" method="post">
<input type="hidden" name="APIName" value="EAPI" />
<input type="hidden" name="APIVersion" value="001.001.002.001" />
<input type="hidden" name="MerReqData" value="<%=strMerReqData %>" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace ICBCBank
{
public partial class SelectOrderInfo : System.Web.UI.Page
{
public string strMerReqData = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
GetOrderInfo();
}
public void GetOrderInfo()
{
StringBuilder strXml = new StringBuilder();
strXml.Append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\"?>");
strXml.Append("<ICBCAPI>");
strXml.Append("<in>");
strXml.Append("<orderNum>2104219229</orderNum>");
strXml.Append("<tranDate>20120331</tranDate>");
strXml.Append("<ShopCode>4000EC20000058</ShopCode>");
strXml.Append("<ShopAccount>4000027219200095989</ShopAccount>");
strXml.Append("</in>");
strXml.Append("</ICBCAPI>");
string errInfo =string.Empty;
CheckOrder("2104219229", "20120331", "4000EC20000058", "4000027219200095989", out errInfo);
strMerReqData = strXml.ToString();
}
/**
* ''' <summary>
''' 工行专用后台Post
''' </summary>
''' <param name="api_url">工行查询地址</param>
''' <param name="APIName">查询接口名称</param>
''' <param name="APIVersion">查询接口版本号</param>
''' <param name="cert_path">证书路径</param>
''' <param name="icbcKey">证书密码</param>
''' <param name="strOrderNum">银行订单号</param>
''' <param name="strTranDate">交易日期(yyyyMMdd)</param>
''' <param name="strShopCode">商户代码</param>
''' <param name="strShopAccount">商户账号</param>
''' <param name="errInfo">异常时的错误信息</param>
''' <returns>返回工行查询结果1、如果查询成功返回Xml文件,如果失败返回5位错误码</returns>
''' <remarks></remarks>
* **/
public static string CheckOrder(string strOrderNum, string strTranDate, string strShopCode, string strShopAccount, out string errInfo)
{
errInfo = string.Empty;
string api_url = "https://corporbank3.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet";
string post_params = "APIName=EAPI&APIVersion=001.001.002.001&MerReqData=";
string cert_path = HttpContext.Current.Server.MapPath("~\\bin\\testsz.pfx");
string cert_password = "12345678";
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);
}
///<summary>
/// SSL
///</summary>
///<param name="post_data"></param>
///<param name="url"></param>
///<param name="cert_path"></param>
///<param name="cert_password"></param>
///<param name="errInfo"></param>
///<returns></returns>
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));
//获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接。
httpRequest.KeepAlive = true;
//获取或设置 User-agentHTTP 标头的值。
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
//获取或设置 Content-typeHTTP 标头的值。
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.Method = "POST";
// 发送 POST 的 BODY数据 要发送到 Internet 资源的数据的字节数
httpRequest.ContentLength = data.Length;
Stream requestStream = httpRequest.GetRequestStream(); //获取用于写入请求数据的 Stream 对象
requestStream.Write(data, 0, data.Length); //发送带有 HttpWebRequest 的数据
requestStream.Close();
Stream responseStream = null; //从 Internet 资源返回数据流
// 获取HTTPs的响应
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;
}
}
}