VB6客户端中如何访问Https的API请求呢?
使用WinHttp组件里的WinHttpRequest对象可以解决我们的问题。
1、引用“Microsoft WinHTTP Services, version 5.1”组件
2、下面是源代码
Public Function HttpsRequest(ByVal strIn As String,ByVal url As String ,ByRef strOut As String) As Boolean
On Error GoTo 0 ' 关闭错误陷阱。
On Error GoTo errexit
'Dim oXmlhttp As MSXML2.XMLHTTP
'Set oXmlhttp = New MSXML2.XMLHTTP
'Dim oXmlhttp As MSXML2.ServerXMLHTTP
'Set oXmlhttp = New MSXML2.ServerXMLHTTP
'https 地址的使用下面的请求方式;XMLHTTP提示指定资源下载失败则使用ServerXMLHTTP;
'使用ServerXMLHTTP提示证书中主名称无效则使用下面方式请求
Dim oXmlhttp As WinHttp.WinHttpRequest
''创建WinHttp.WinHttpRequest
Set oXmlhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
'' 忽略调用错误
oXmlhttp.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = &H3300
'同步接收数据
oXmlhttp.Open "post", url, False
'其它请求头设置
oXmlhttp.setRequestHeader "accept-encoding", "gzip,deflate"
oXmlhttp.setRequestHeader "content-type", "application/x-www-form-urlencoded;charset=UTF-8"
'发送数据请求
oXmlhttp.send strIn
'得到返回数据,这里根据情况确定
strOut = oXmlhttp.responseText
HttpsRequest= True
Set oXmlhttp = Nothing
Exit Function
errexit:
Set oXmlhttp = Nothing
MsgBox "调用失败:" & Err.Description, vbInformation + vbOKOnly, "瀚文软件"
Err.Clear
HttpsRequest = False
End Function