今天帮同事做另一家公司的Web系统数据对接,我方发送请求,对方返回一些数据,用的是客户端用的是VB6,代码记录如下:
Dim postData As String Dim url As String url = "http://111.11.11.11/services/getQuickInfoService" postData = TextContent.Text '--- post Dim HttpClient As Object Set HttpClient = CreateObject("Microsoft.XMLHTTP") HttpClient.Open "POST",url,False HttpClient.setRequestHeader "Content-Type","text/xml; charset=UTF-8" HttpClient.Send pvToByteArray(postData) Do While HttpClient.readyState <> 4 DoEvents Loop TextResponse.Text = HttpClient.responseText MsgBox "发送完毕"
下面是发送内容的编码部分:
' 下面是两个转换函数 Private Function pvToByteArray(sText As String) As Byte() pvToByteArray = GB2312ToUTF8(sText) End Function Public Function GB2312ToUTF8(strIn As String,Optional ByVal ReturnValueType As VbVarType = vbString) As Variant Dim adoStream As Object Set adoStream = CreateObject("ADODB.Stream") adoStream.Charset = "utf-8" adoStream.Type = 2 'adTypeText adoStream.Open adoStream.WriteText strIn adoStream.Position = 0 adoStream.Type = 1 'adTypeBinary GB2312ToUTF8 = adoStream.Read() adoStream.Close If ReturnValueType = vbString Then GB2312ToUTF8 = Mid(GB2312ToUTF8,1) End Function
效果: