How to display ResponseText in UTF-8 format? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

How to display ResponseText in UTF-8 format?

02 Jun 2019, 03:28

When whr.ResponseText contains UTF-8 text, garbled characters may be displayed.

I have read this post,
https://www.autohotkey.com/boards/viewtopic.php?f=5&t=16083

UrlDownloadToFile can be recognized correctly. Is there a solution for WinHttp?

For example,

Code: Select all

whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "https://ipmsg.org/protocol.txt", true)
whr.Send()
whr.WaitForResponse()
MsgBox % whr.ResponseText
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to display ResponseText in UTF-8 format?  Topic is solved

02 Jun 2019, 13:11

Use ResponseBody instead of ResponseText:

Code: Select all

whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "https://ipmsg.org/protocol.txt", true)
whr.Send()
whr.WaitForResponse()
arr := whr.responseBody
pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize)
length := arr.MaxIndex() + 1
MsgBox, % text := StrGet(pData, length, "utf-8")
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to display ResponseText in UTF-8 format?

03 Jun 2019, 01:59

Wonderful!

Why to add 8 here?

Code: Select all

NumGet(ComObjValue(arr) + 8 + A_PtrSize)
Thank you very much!
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to display ResponseText in UTF-8 format?

03 Jun 2019, 04:28

afe wrote: Why to add 8 here?
ComObjValue(arr) returns the pointer to SAFEARRAY structure:

Image

We need to get the pointer to pvData. USHORT + USHORT + ULONG = 8 bytes; the next USHORT = 4 bytes, but since pvData contains a pointer, it must be on the boundary, which is multiple of A_PtrSize, so instead of 4 we add A_PtrSize.
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to display ResponseText in UTF-8 format?

05 Jun 2019, 08:32

teadrinker wrote: USHORT + USHORT + ULONG = 8 bytes; the next USHORT = 4 bytes
There is a typo here, should be
USHORT + USHORT + ULONG = 8 bytes; the next ULONG = 4 bytes
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: How to display ResponseText in UTF-8 format?

28 Aug 2022, 10:13

And here is the code for AutoHotkey V2.

Code: Select all

whr := ComObject("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "http://some/url", true)
whr.Send()
whr.WaitForResponse()
arr := Whr.responseBody
pData := NumGet(ComObjValue(arr)+8 +A_PtrSize,0,"UPtr")
length := (arr.MaxIndex() - arr.MinIndex()) + 1
response := StrGet(pData,  "utf-8")
Hopefully correct.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Descolada and 130 guests