VB的HttpPost请求
注意:URL最后要“/”结尾,否则会导致格式不正确无法正常接收。我就在这里踩过坑,不加“/”服务端就是收不到POST数据,后面才发现加了“/”就可以收到了。函数使用示列:HttpPost(“http://172.20.11.114/KormeeDbHicky/devpsot/","data1=88&data2=99”)下面是VB6的HTTP POST 发送数据的代码:'函数返回值是...
·
注意:URL最后要“/”结尾,否则会导致格式不正确无法正常接收。我就在这里踩过坑,不加“/”服务端就是收不到POST数据,后面才发现加了“/”就可以收到了。
函数使用示列:
HttpPost(“http://172.20.11.114/KormeeDbHicky/devpsot/","data1=88&data2=99”)
下面是VB6的HTTP POST 发送数据的代码:
'函数返回值是返回信息
'Url:发送的Url地址
'PostMsg:要发送的数据
Function HttpPost(Url As String, PostMsg As String) As String
On Error GoTo er
Dim XMLHTTP As Object
Set XMLHTTP = CreateObject("Msxml2.XMLHTTP")
If Not IsObject(XMLHTTP) Then
Set XMLHTTP = CreateObject("Microsoft.XMLHTTP")
If Not IsObject(XMLHTTP) Then Exit Function
End If
XMLHTTP.Open "POST", Url, False
XMLHTTP.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
'XMLHTTP.send PostMsg
XMLHTTP.send UTF8EncodeURI(PostMsg)
Do While XMLHTTP.ReadyState <> 4
DoEvents
Loop
If XMLHTTP.Status = 200 Then
HttpPost = XMLHTTP.ResponseText
Else
HttpPost = ""
End If
Exit Function
er:
MsgBox "发送POST请求失败!", , "提示"
End Function
'发送的内容转为utf8
Public Function UTF8EncodeURI(szInput)
Dim wch, uch, szRet
Dim X
Dim nAsc, nAsc2, nAsc3
If szInput = "" Then
UTF8EncodeURI = szInput
Exit Function
End If
For X = 1 To Len(szInput)
wch = Mid(szInput, X, 1)
nAsc = AscW(wch)
If nAsc < 0 Then nAsc = nAsc + 65536
If (nAsc And &HFF80) = 0 Then
szRet = szRet & wch
Else
If (nAsc And &HF000) = 0 Then
uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
szRet = szRet & uch
Else
uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
Hex(nAsc And &H3F Or &H80)
szRet = szRet & uch
End If
End If
Next
UTF8EncodeURI = szRet
End Function
扫码下方二维码关注本人公众号
分享编程相关知识,共同进步吧。工众号回复【我要资源】
免费获取
,获取更多学习资源及视频教程 (Linux\Python\Java\PHP\Labview和大数据等视频资料)。联系邮箱:898109004@qq.com
更多推荐
已为社区贡献3条内容
所有评论(0)