Re: XML JSON UTF-8 problem
A string variable can not contain UTF8
Code:
'---------------------------------------------------------------------------------------
' Module : modUTF8.bas
' DateTime : 20-9-2011
' Author : ActiveVB http://www.activevb.de/rubriken/faq/faq0155.html
' Purpose :
'---------------------------------------------------------------------------------------
Option Explicit
Private Declare Function WideCharToMultiByte Lib "kernel32.dll" ( _
ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByVal lpWideCharStr As Long, _
ByVal cchWideChar As Long, _
ByVal lpMultiByteStr As Long, _
ByVal cbMultiByte As Long, _
ByVal lpDefaultChar As Long, _
ByVal lpUsedDefaultChar As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32.dll" ( _
ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByVal lpMultiByteStr As Long, _
ByVal cbMultiByte As Long, _
ByVal lpWideCharStr As Long, _
ByVal cchWideChar As Long) As Long
Private Const CP_UTF8 As Long = 65001
Public Function ConvertToUTF8(ByRef Source As String) As Byte()
Dim Length As Long
Dim Pointer As Long
Dim Size As Long
Dim Buffer() As Byte
If Len(Source) > 0 Then
Length = Len(Source)
Pointer = StrPtr(Source)
Size = WideCharToMultiByte(CP_UTF8, 0, Pointer, Length, 0, 0, 0, 0)
If Size > 0 Then
ReDim Buffer(0 To Size - 1)
WideCharToMultiByte CP_UTF8, 0, Pointer, Length, VarPtr(Buffer(0)), Size, 0, 0
ConvertToUTF8 = Buffer
End If
End If
End Function
Public Function ConvertFromUTF8(ByRef Source() As Byte) As String
Dim Size As Long
Dim Pointer As Long
Dim Length As Long
Dim Buffer As String
Size = UBound(Source) - LBound(Source) + 1
Pointer = VarPtr(Source(LBound(Source)))
If pUTF8header(Source) Then
' Ignore BOM header
' http://en.wikipedia.org/wiki/Byte_order_mark
Size = Size - 3
Pointer = Pointer + 3
End If
Length = MultiByteToWideChar(CP_UTF8, 0, Pointer, Size, 0, 0)
Buffer = Space$(Length)
MultiByteToWideChar CP_UTF8, 0, Pointer, Size, StrPtr(Buffer), Length
ConvertFromUTF8 = Buffer
End Function
'---------------------------------------------------------------------------------------
' Procedure : pUTF8header
' DateTime : 14-6-2013
' Reference : http://en.wikipedia.org/wiki/Byte_order_mark
'---------------------------------------------------------------------------------------
Private Function pUTF8header(Source() As Byte) As Boolean
If UBound(Source) >= 2 Then
If Source(0) = &HEF Then
If Source(1) = &HBB Then
If Source(2) = &HBF Then
pUTF8header = True
Exit Function
End If
End If
End If
End If
End Function
Re: XML JSON UTF-8 problem
How to implement this in my code from 1. post?
Re: XML JSON UTF-8 problem
As it says in the Fine Manual:
Quote:
send Method (ServerXMLHTTP/IServerXMLHTTPRequest)
If the input type is a BSTR, the response is always encoded as UTF-8. The caller must set a Content-Type header with the appropriate content type and include a charset parameter.
Both XML and JSON can handle Unicode in string literals as long as you have escaped their respective reserved characters.
Then it becomes a question of how standards compliant both ends are. If the server is being as hackish as your client code, fiddling with the raw text directly, all bets are pretty much off.
You might at least try sending the Content-Type as application/json; charset=utf-8 though, and see whether it helps. In many HTTP clients and servers the default encoding is first 7-bit ASCII, then if that fails the current ANSI codepage is tried. The exact heuristics followed depends on each specific implementation.
But I can't see how trying to replicate what ServerXMLHTTP.send already does when fed a String is going to make any difference here.
Re: XML JSON UTF-8 problem
Quote:
Originally Posted by
Davor Geci
How to implement this in my code from 1. post?
Try something like this
Code:
objHTTP.setRequestHeader "Content-type", "application/json; charset=utf-8" '--- dilettante's idea
objHTTP.send ConvertToUTF8(strJSON) '--- Arnoutdv's ides
cheers,
</wqw>
Re: XML JSON UTF-8 problem
This worked like a charm.
Thanks for good solution.