A
A
Ai Lab2016-01-18 13:05:04
Programming
Ai Lab, 2016-01-18 13:05:04

How to read text correctly in VBA?

I am using the following function:

Option Explicit
Function GetData(ByVal api As String, ByVal content As String)
Restart: Dim sUrl As String
sUrl = "http://127.0.0.1/api/" + api
Dim oRequest As Object
Dim sResult As String
Dim sResult2 As String
On Error GoTo Err_DoSomeJob
 
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
With oRequest
    .Open "GET", sUrl, True
    .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=windows-1251"
    .Send content
    .WaitForResponse
    sResult = .ResponseText
    Debug.Print sResult
    sResult2 = .Status
    Debug.Print sResult2
End With
 
Exit_DoSomeJob:
    On Error Resume Next
    GetData = sResult
    Set oRequest = Nothing
    If sResult2 <> "200" Then
        GoTo Restart
    End If
    Exit Function
   
Err_DoSomeJob:
    'MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_DoSomeJob
End Function

It works fine, but the text that it returns looks incorrect, as if with spaces between characters, but in fact there is nothing between them, it's probably something to do with the encoding, but I tried to send in two different ones and I still get the same thing, maybe someone know how to deal with this?
The essence of the macro is as follows, a request is sent to a self-made http server, which receives text and parameters from the url and somehow changes it and sends it back, everything is fine, but what comes is not quite what it sent.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2016-01-18
@SaNNy32

Apparently the text comes in Unicode encoding. Try converting text with StrConv

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question