VB6 http post 发送与返回内容处理

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhouyingge1104/article/details/50186007

今天帮同事做另一家公司的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


效果:



VB6比较老了,没有Java和C#那么完备,也没什么插件可以实现特定功能,但任然可以用原始的语句写出很多功能。

展开阅读全文

没有更多推荐了,返回首页