Jump to content

WinHttpReqest.ResponseBody doesn't work


 Share

Recommended Posts

Edited: removed an example of nonsense...

My question is: how to obtain information from the WinHttpRequest.ResponseBody property in AutoIt script?

I don't need ResponseText because it uses wrong code page on non-latin html. As a result, the returned content is unreadable. To correctly handle this, an instance of ADODB.Stream must be used to receive the array of ResponseBody. But it seems that there's something wrong between WinHttpReqest.ResponseBody & AutoIt, while everything's normal using WSH.

Edited: removed some naggings...

ps. similar code in VBScript

set winhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
winhttp.open "GET","http://www.pchome.com.tw/",false
winhttp.send
WScript.Echo winhttp.responseText   'contains unreadable kanzi characters
set oStream = CreateObject("ADODB.Stream")
oStream.Type=1
oStream.Mode=3
oStream.Open() 
oStream.Write(winhttp.responseBody)
oStream.Position=0
oStream.Type=2
oStream.Charset="big5"
result= oStream.ReadText()
WScript.Echo result   'normal content

Please see examples below...

Edited by CatchFish
Link to comment
Share on other sites

Instead of showing something "similar" in VBS, perhaps you could show "exactly" the same example implemented in VBS. That would allow focus on exactly where the differences in results lie and their cause.

thanks,

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

The "exactly" the same example is here

Dim $winhttp = ObjCreate("WinHttp.WinHttpRequest.5.1")
$winhttp.open("GET","http://www.pchome.com.tw/",false)
$winhttp.send
ConsoleWrite($winhttp.responseText & @LF)   ;contains unreadable kanzi characters
Dim $oStream = ObjCreate("ADODB.Stream")
$oStream.Type=1
$oStream.Mode=3
$oStream.Open()
$oStream.Write($winhttp.responseBody)   ;here occurs a COM error, and script exits
$oStream.Position=0
$oStream.Type=2
$oStream.Charset="big5"
$result= $oStream.ReadText()
ConsoleWrite($result & @LF)

The COM error occurs because there's SOMETHING wrong with $winhttp.responseBody in AutoIt.

Let's see another example. Here's the code in vb script:

set winhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
winhttp.open "GET","http://www.pchome.com.tw/",false
winhttp.send
WScript.Echo MidB(winhttp.responseBody, 2, 1)   'returns "h" of "<html>..."

And code in AutoIt:

Dim $winhttp = ObjCreate("WinHttp.WinHttpRequest.5.1")
$winhttp.open("GET","http://www.pchome.com.tw/",false)
$winhttp.send
ConsoleWrite(StringMid($winhttp.responseBody, 2, 1) & @LF)   ;here returns NOTHING

Seems that AutoIt can't handle responseBody property well. And here's the documentation from MSDN for reference:

The ResponseBody property retrieves the response entity body as an array of unsigned bytes.

...This array contains the raw data as received directly from the server.

http://msdn.microsoft.com/library/en-us/wi...esponsebody.asp
Link to comment
Share on other sites

It actually fills the array... over 80,000 characters. If they are valid, perhaps someone knows a trick or two about converting them to displayable form in AutoIt?

Here is modified code:

Dim $winhttp = ObjCreate("WinHttp.WinHttpRequest.5.1")
$winhttp.open("GET","http://www.pchome.com.tw/",false)
$winhttp.send
$rb = $winhttp.responseBody
ConsoleWrite("Array: " & IsArray($rb) & ", ElemntCount: " & Ubound($rb) & @CR & "Value: <" & Hex(StringMid($rb, 2, 1), 8) & ">" & @CR)

Here's the result:

>Running: (3.1.1.87):C:\Program Files\AutoIt3\beta\autoit3.exe "C:\Test.au3"    
Array: 1, ElemntCount: 81582
Value: <00000000>

Dale

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...