views:

34

answers:

1

I'm trying to use sockets to connect to a server through HTA scripting. Is this possible? There's isn't much information on the web, some examples would be greatly appreciated.

+1  A: 

There is no socket access built into JScript, but you can use any of the installed ActiveX components.

I guess there are some 3rd party ones that provide raw socket access, but there are none in vanilla Windows installations. This means you'd not only have to distribute the HTA, but also install an ActiveX component, which would make deployment rather cumbersome.

If you can, use HTTP as a way to access remote resources, and use the built-in XmlHttpRequest or WinHttpRequest objects that comes with Windows.

Tomalak
Unfortunately I can't use the ActiveX HTTP component to access remote HTTP servers as I get an Access denied because of the `same-origin` policy. Any ideas?
Luca Matteis
@Luca: I'm pretty sure a WinHttpRequest object can make any request you want.
Tomalak
@Tomalak: That's what I thought as well. However I get this error when using the WinHTTPRequest component. `The connection with the server was terminated abnormally`. Here's the piece of code I'm running: http://codepad.org/gKIxwJEi
Luca Matteis
@Luca: Maybe the proxy settings are missing? To find out what's really going on, it's easiest to use a network sniffer and analyze the traffic. Should be pretty easy to pin this problem.
Tomalak
@Tomalak, tried using Wireshark, no HTTP packets are initiated when I run that code. So far I'm at a lost case... there's like no documentation for this on the web. Any help would be greatly appreciated.
Luca Matteis
@Luca: Try looking for TCP packets. The "connection terminated abnormally" message indicates there *must be* some network traffic going on. My bet is still on the missing proxy settings. IIRC, the WinHttpRequest object does not care for your Internet Explorer settings and must be configured separately through code.
Tomalak
@Tomalak: this might be it, however I can't find information on the API to use for proxy configuration. And my browsers don't have any weird Proxy configuration... just default stuff.
Luca Matteis
@Luca: Does "default stuff" mean there is no proxy? PS: Why did you accept the answer?
Tomalak
@Tomalak, because you've answered my question about the Sockets... this extra HTTP talk is extra I guess... but anyway yeah by "default stuff" I mean that there's no proxy :)
Luca Matteis
@Luca: For the record: I've tried WinHttpRequest and it works perfectly for me.
Tomalak