2

I'm trying to download this test page http://173.192.48.92/test.php using JScript under WSH.

I'm using the WinHttp component like this:

WinHTTP = WScript.CreateObject('WinHttp.WinHttpRequest.5.1') ;
WinHTTP.Open( 'GET', 'http://173.192.48.92/test.php' ) ;
WinHTTP.Send() ;
WScript.echo( WinHTTP.responseText ) ;

But I cannot make it work. WinHttp fires a run-time error because the page has Content-Type: text/html; charset=UTF-8 but the actual content of the page is in latin1.

Is there a way I can get the content of a URL using WSH no matter what that content is?

PS: I'm using Windows Vista SP2 and IE9

GetFree
  • 40,278
  • 18
  • 77
  • 104

1 Answers1

2

I'm not sure whether I'll resolve your issue but at least will try.

The IE version not matter when you run scripts in WSH as they use separate scripting hosts. Your script works just fine under my XP x64.

Ok, what I have in mind is to try XMLHttpRequest object. I'll post VBScript code, and I think it's not hard to convert it to JavaScript if necessary.

URL = "http://173.192.48.92/test.php"
Set http = CreateObject("Microsoft.XmlHttp")

http.open "GET", URL, False
http.send ""

If http.status <> 200 Then 'if not OK
    WScript.Echo "Response status " & _
        http.status & " - " & http.statusText
    WScript.Quit  'exit the script
End If

WScript.Echo http.responseText

If the encoding is the issue then you should get an error on the last line. If so, then remove this line and try with ADO Stream object, i.e. append this to above code:

Set stream = CreateObject("ADODB.Stream")
With stream
    .Open
    .Type     = 2 'text
    .Position = 0
    '.Charset  = "utf-8"
    .Charset  = "latin1"
    .WriteText http.responseText
    .SaveToFile "output.txt", 2
    .Close
End With

In my test it works with both charsets UTF-8 and Latin1, and I hope this w'd be the cure.

Well, if that not help, then let me know that kind of errors you get.

Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28
  • 1
    The `Microsoft.XmlHttp` object seems to have no problems with encoding errors. The `WinHttp.WinHttpRequest` object, however, seems to have an impossible-to-workaround problem with encoding error from WinVista on (on Win XP it works fine). – GetFree Mar 02 '13 at 05:25