You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB6应用调用HTTPS POST请求实现SMS发送API遇308重定向问题求助

VB6调用HTTPS短信API解决方案

问题核心

你遇到的308永久重定向,是因为原请求用的是HTTP协议,服务商要求改用HTTPS,服务器将HTTP请求重定向到HTTPS地址。Postman会自动处理重定向所以能正常运行,但VB6原生WinSock不支持HTTPS,也不会自动处理重定向,这就是问题根源。

解决方案:使用支持HTTPS的控件

VB6原生WinSock不支持SSL/TLS加密,需要改用系统自带的支持HTTPS的COM控件,推荐两种实现方式:

方法1:使用MSXML2.XMLHTTP

Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0") ' 优先用6.0版本,兼容性更好

' 直接请求HTTPS地址,避免308重定向
xmlHttp.Open "POST", "https://api.africastalking.com/version1/messaging", False

' 设置请求头
xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlHttp.setRequestHeader "ApiKey", "37236e3e47654ee7773bbbc78a4049fde7d3390dbb70f630b19f4aeab7b0ce88"

' 构造表单数据(特殊字符需URL编码)
Dim postData As String
postData = "username=xxxxxxx" & _
           "&message=" & URLEncode("Hello Kind regards, xxx xxxx xxx. Tel. 0721000111") & _
           "&to=%2B254721000111" & _
           "&from=xxxx"

' 发送请求
xmlHttp.send postData

' 处理响应
If xmlHttp.Status = 200 Then
    MsgBox "请求成功:" & xmlHttp.responseText
Else
    MsgBox "请求失败,状态码:" & xmlHttp.Status & vbCrLf & xmlHttp.responseText
End If

Set xmlHttp = Nothing

' 自定义URL编码函数(VB6客户端无内置编码方法)
Function URLEncode(ByVal str As String) As String
    Dim bytes() As Byte
    Dim i As Integer
    bytes = StrConv(str, vbUnicode)
    For i = 0 To UBound(bytes) Step 2
        Dim charCode As Integer
        charCode = bytes(i)
        Select Case charCode
            Case 48 To 57, 65 To 90, 97 To 122, 45, 46, 95, 126
                URLEncode = URLEncode & Chr(charCode)
            Case 32
                URLEncode = URLEncode & "+"
            Case Else
                URLEncode = URLEncode & "%" & Right("0" & Hex(charCode), 2)
        End Select
    Next i
End Function

方法2:使用WinHttp.WinHttpRequest

Dim winHttp As Object
Set winHttp = CreateObject("WinHttp.WinHttpRequest.5.1")

' 打开HTTPS请求
winHttp.Open "POST", "https://api.africastalking.com/version1/messaging", False

' 设置请求头
winHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttp.setRequestHeader "ApiKey", "37236e3e47654ee7773bbbc78a4049fde7d3390dbb70f630b19f4aeab7b0ce88"

' 构造表单数据(特殊字符需URL编码)
Dim postData As String
postData = "username=xxxxxxx" & _
           "&message=" & URLEncode("Hello Kind regards, xxx xxxx xxx. Tel. 0721000111") & _
           "&to=%2B254721000111" & _
           "&from=xxxx"

' 发送请求
winHttp.send postData

' 处理响应
If winHttp.Status = 200 Then
    MsgBox "请求成功:" & winHttp.responseText
Else
    MsgBox "请求失败,状态码:" & winHttp.Status & vbCrLf & winHttp.responseText
End If

Set winHttp = Nothing

' 复用上述URLEncode函数
Function URLEncode(ByVal str As String) As String
    Dim bytes() As Byte
    Dim i As Integer
    bytes = StrConv(str, vbUnicode)
    For i = 0 To UBound(bytes) Step 2
        Dim charCode As Integer
        charCode = bytes(i)
        Select Case charCode
            Case 48 To 57, 65 To 90, 97 To 122, 45, 46, 95, 126
                URLEncode = URLEncode & Chr(charCode)
            Case 32
                URLEncode = URLEncode & "+"
            Case Else
                URLEncode = URLEncode & "%" & Right("0" & Hex(charCode), 2)
        End Select
    Next i
End Function

关键注意事项

  • 直接请求HTTPS地址:不要用原HTTP地址,避免触发308重定向,直接请求HTTPS更高效。
  • URL编码:所有含特殊字符(空格、逗号、句号等)的参数必须编码,否则会导致参数解析错误。
  • 控件版本:优先使用高版本控件,确保支持TLS 1.2及以上协议(多数服务商已淘汰旧版SSL)。
  • 网络权限:确保VB6程序有足够网络权限访问目标HTTPS地址。

内容的提问来源于stack exchange,提问作者user2614086

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:23:08