Win10下VB6程序调用WinHttpRequest访问xeno-canto API失败求助
解决方案:VB6在Win10下实现TLS 1.3 HTTP通信
你的问题核心是Windows 10自带的WinHttpRequest 5.1组件不支持TLS 1.3,而xeno-canto已切换为仅支持TLS 1.3协议,导致SSL握手失败抛出RTE -2147012739错误。以下是三种可行的适配方案:
方案1:使用WebView2控件(推荐)
WebView2基于Chromium内核,天然支持TLS 1.3,兼容性强且免费可用。
步骤:
- 安装WebView2 Runtime(Win10需手动下载安装,Win11通常预装)
- 在VB6工程中,通过「工程→部件」勾选「Microsoft Edge WebView2 Control」,添加到窗体(命名为
wv2)
示例代码:
Private Sub Form_Load() ' 初始化WebView2 Set wv2.CoreWebView2InitializationCompleted = AddressOf CoreWebView2_InitCompleted wv2.EnsureCoreWebView2Async vbNullString End Sub Private Sub CoreWebView2_InitCompleted(sender As Object, args As CoreWebView2InitializationCompletedEventArgs) If args.IsSuccess Then ' 通过Fetch API发送GET请求,将响应转为Base64传回VB6 Dim fetchScript As String fetchScript = "fetch('https://xeno-canto.org/api/2/recordings?query=Flammulated+Flycatcher%20q:A')" & _ ".then(res => res.arrayBuffer())" & _ ".then(buf => {" & _ " const b64 = btoa(new Uint8Array(buf).reduce((s, b) => s + String.fromCharCode(b), ''));" & _ " window.chrome.webview.postMessage(b64);" & _ "})" wv2.CoreWebView2.ExecuteScriptAsync fetchScript Else MsgBox "WebView2初始化失败: " & args.InitializationError.Message End If End Sub Private Sub wv2_WebMessageReceived(sender As Object, args As CoreWebView2WebMessageReceivedEventArgs) ' 解码Base64为字节数组 Dim base64Str As String base64Str = Replace(Replace(args.WebMessageAsJson, """", ""), "\", "") Dim responseBytes() As Byte responseBytes = DecodeBase64(base64Str) ' 后续处理响应数据(如解析、保存等) MsgBox "成功获取到" & UBound(responseBytes) + 1 & "字节数据" End Sub ' VB6 Base64解码辅助函数 Private Function DecodeBase64(ByVal base64Str As String) As Byte() Dim xmlDoc As Object Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") Dim tmpNode As Object Set tmpNode = xmlDoc.createElement("tmp") tmpNode.DataType = "bin.base64" tmpNode.Text = base64Str DecodeBase64 = tmpNode.nodeTypedValue End Function
方案2:调用cURL命令行工具
无需引入控件,直接调用curl.exe(需将最新版curl.exe放在程序目录或系统PATH中),强制使用TLS 1.3发起请求。
示例代码:
Public Function DownloadWithCURL(url As String) As Byte() Dim shellObj As Object Dim tempFilePath As String tempFilePath = Environ("TEMP") & "\curl_temp_response.bin" Set shellObj = CreateObject("WScript.Shell") ' 执行curl命令,强制TLS1.3,将响应保存到临时文件 shellObj.Run "cmd /c curl -L --tlsv1.3 """ & url & """ -o """ & tempFilePath & """", 0, True ' 读取临时文件为字节数组 Dim fileNum As Integer fileNum = FreeFile Open tempFilePath For Binary Access Read As #fileNum ReDim DownloadWithCURL(LOF(fileNum) - 1) Get #fileNum, , DownloadWithCURL Close #fileNum ' 清理临时文件 Kill tempFilePath Set shellObj = Nothing End Function
方案3:使用第三方商业控件(如Chilkat HTTP)
Chilkat提供成熟的COM组件,支持TLS 1.3,功能全面但需授权使用。
示例代码:
Public Function DownloadWithChilkat(url As String) As Byte() Dim httpObj As Object Set httpObj = CreateObject("Chilkat.Http") ' 强制使用TLS1.3 httpObj.TlsVersion = "1.3" ' 发起GET请求并获取字节数组响应 Dim success As Boolean Dim responseBytes() As Byte success = httpObj.DownloadB(url, responseBytes) If success Then DownloadWithChilkat = responseBytes Else MsgBox "请求失败: " & httpObj.LastErrorText End If Set httpObj = Nothing End Function
内容的提问来源于stack exchange,提问作者JJJones_3860
相关产品推荐
相关产品推荐

