工行的测试环境真是非常地烂,给的api查询居然是c++的,还写着dotnet的名字,这里我给出一个asp.net c#的实现
public
class
ICBC_API
{
static string apiUrl = " https://corporbank3.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet " ;
static string cerPath = ConfigurationManager.AppSettings[ " CertPath " ];
static string cerPwd = ConfigurationManager.AppSettings[ " CertPwd " ];
public ICBC_API()
{
}
/// <summary>
/// 订单查询
/// </summary>
/// <param name="orderNum"> 订单号 </param>
/// <param name="tradeDate"> 订单日期(yyyyMMdd) </param>
/// <param name="shopCode"> 商户代码 </param>
/// <param name="shopAccount"> 商户帐号 </param>
/// <returns></returns>
public static string B2C( string orderNum, string tradeDate, string shopCode, string shopAccount)
{
// gen post data
string postParams = " APIName=EAPI&APIVersion=001.001.002.001&MerReqData={0} " ;
StringBuilder sb = new StringBuilder();
sb.Append( " <?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?><ICBCAPI><in><orderNum> " );
sb.Append(orderNum);
sb.Append( " </orderNum><tranDate> " );
sb.Append(tradeDate);
sb.Append( " </tranDate><ShopCode> " );
sb.Append(shopCode);
sb.Append( " </ShopCode><ShopAccount> " );
sb.Append(shopAccount);
sb.Append( " </ShopAccount></in></ICBCAPI> " );
string postData = string .Format(postParams, sb.ToString());
// 验证证书,默认有效
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
// 配置请求参数
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(apiUrl);
wReq.ContentType = " application/x-www-form-urlencoded " ;
wReq.Method = " POST " ;
wReq.ClientCertificates.Add( new X509Certificate2(HttpContext.Current.Server.MapPath(cerPath), cerPwd));
ASCIIEncoding encoding = new ASCIIEncoding();
byte [] data = encoding.GetBytes(postData);
wReq.ContentLength = data.Length;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(data, 0 , data.Length);
reqStream.Close();
// 获取结果
WebResponse wResp = wReq.GetResponse();
Stream respStream = wResp.GetResponseStream();
string stringResp = string .Empty;
if (respStream != null )
{
using (StreamReader respReader = new StreamReader(respStream,Encoding.GetEncoding( " GBK " )))
{
stringResp = respReader.ReadToEnd();
}
respStream.Close();
}
return stringResp;
}
}
{
static string apiUrl = " https://corporbank3.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet " ;
static string cerPath = ConfigurationManager.AppSettings[ " CertPath " ];
static string cerPwd = ConfigurationManager.AppSettings[ " CertPwd " ];
public ICBC_API()
{
}
/// <summary>
/// 订单查询
/// </summary>
/// <param name="orderNum"> 订单号 </param>
/// <param name="tradeDate"> 订单日期(yyyyMMdd) </param>
/// <param name="shopCode"> 商户代码 </param>
/// <param name="shopAccount"> 商户帐号 </param>
/// <returns></returns>
public static string B2C( string orderNum, string tradeDate, string shopCode, string shopAccount)
{
// gen post data
string postParams = " APIName=EAPI&APIVersion=001.001.002.001&MerReqData={0} " ;
StringBuilder sb = new StringBuilder();
sb.Append( " <?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?><ICBCAPI><in><orderNum> " );
sb.Append(orderNum);
sb.Append( " </orderNum><tranDate> " );
sb.Append(tradeDate);
sb.Append( " </tranDate><ShopCode> " );
sb.Append(shopCode);
sb.Append( " </ShopCode><ShopAccount> " );
sb.Append(shopAccount);
sb.Append( " </ShopAccount></in></ICBCAPI> " );
string postData = string .Format(postParams, sb.ToString());
// 验证证书,默认有效
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
// 配置请求参数
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(apiUrl);
wReq.ContentType = " application/x-www-form-urlencoded " ;
wReq.Method = " POST " ;
wReq.ClientCertificates.Add( new X509Certificate2(HttpContext.Current.Server.MapPath(cerPath), cerPwd));
ASCIIEncoding encoding = new ASCIIEncoding();
byte [] data = encoding.GetBytes(postData);
wReq.ContentLength = data.Length;
Stream reqStream = wReq.GetRequestStream();
reqStream.Write(data, 0 , data.Length);
reqStream.Close();
// 获取结果
WebResponse wResp = wReq.GetResponse();
Stream respStream = wResp.GetResponseStream();
string stringResp = string .Empty;
if (respStream != null )
{
using (StreamReader respReader = new StreamReader(respStream,Encoding.GetEncoding( " GBK " )))
{
stringResp = respReader.ReadToEnd();
}
respStream.Close();
}
return stringResp;
}
}