Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Security.Cryptography.X509Certificates
Imports System.Net.Security
Public Class IcbcPost
''' <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 Function CheckOrder(ByVal api_url As String, ByVal APIName As String, ByVal APIVersion As String, ByVal cert_path As String, ByVal icbcKey As String, ByVal strOrderNum As String, ByVal strTranDate As String, ByVal strShopCode As String, ByVal strShopAccount As String, ByRef errInfo As String) As String
errInfo = String.Empty
Dim post_params As String = "APIName=" + APIName + "&APIVersion=" + APIVersion + "&MerReqData="
Dim sb As StringBuilder = 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>")
Dim post_data As String = post_params + sb.ToString()
Return PostDataBySSL(post_data, api_url, cert_path, icbcKey, errInfo)
End Function
Private Function PostDataBySSL(ByVal post_data As String, ByVal url As String, ByVal cert_path As String, ByVal cert_password As String, ByRef errInfo As String) As String
errInfo = String.Empty
Try
Dim encoding As ASCIIEncoding = New ASCIIEncoding()
Dim data As Byte() = encoding.GetBytes(post_data)
If cert_path <> String.Empty Then
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
End If
Dim WebRequest As WebRequest = WebRequest.Create(url)
Dim httpRequest As HttpWebRequest = TryCast(WebRequest, HttpWebRequest)
If (cert_path.ToLower().EndsWith(".cer")) Then
httpRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile(cert_path))
Else
httpRequest.ClientCertificates.Add(New X509Certificate2(cert_path, cert_password))
End If
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
Dim requestStream As Stream = httpRequest.GetRequestStream()
requestStream.Write(data, 0, data.Length)
requestStream.Close()
Dim responseStream As Stream = Nothing
responseStream = httpRequest.GetResponse().GetResponseStream()
Dim stringResponse As String = String.Empty
If responseStream IsNot Nothing Then
Using responseReader As StreamReader = New StreamReader(responseStream, Text.Encoding.GetEncoding("GBK"))
stringResponse = responseReader.ReadToEnd()
End Using
responseStream.Close()
End If
Return stringResponse
Catch e As Exception
errInfo = e.Message
Return String.Empty
End Try
End Function
Private Function ValidateServerCertificate(ByVal sender As Object, ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyerrors As SslPolicyErrors) As Boolean
Return True
End Function
End Class
VB.NET版ssl链接(工行查询接口的实现)
最新推荐文章于 2020-05-24 01:50:57 发布