digip Posted September 14, 2008 Posted September 14, 2008 Complete windows http get request a file done with JS Bat and Cscript for windows. (Credit to Microsoft for this http://msdn.microsoft.com/en-us/library/aa384071(VS.85).aspx ) I was looking for a way to do http requests in VB6 when I came across some code on MSDN that shows how to perform an HTTP request using just some simple Javascript. What is this good for? Basically this only downloads html and text files at the moment, but I am working on doing EXE's and other binary files, like mp3's, etc. When calling the Javascript file using Cscript, it will execute the Javascript and pull in any file you point the script to(Being html of course). I then decided to add it to a bat file so you can just run it and it prompts you for the file you want to download as well as what you want to name it on your end. Create two files. One called get.txt and another whateveryouwant.bat (Rar file of everything you'll need: http://www.twistedpairrecords.com/digip/Win_HTTP_request.rar ) get.txt : function getText(strURL) { var strResult; try { // Create the WinHTTPRequest ActiveX Object. var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); // Create an HTTP request. var temp = WinHttpReq.Open("GET", strURL, false); // Send the HTTP request. WinHttpReq.Send(); // Retrieve the response text. strResult = WinHttpReq.ResponseText; } catch (objError) { strResult = objError + "\n" strResult += "WinHTTP returned error: " + (objError.number & 0xFFFF).toString() + "\n\n"; strResult += objError.description; } // Return the response text. return strResult; } //WScript.Echo(getText("http://www.microsoft.com/default.htm")); <<--Commented out as we are going to create this from the bat script!! >> //Make sure the last line is an extra blank line!! The forums remove white space in the script above, so if you copy and paste the above code, make sure you add two extra blank lines in the file before saving it or it will not work!! httpGet.bat : :: WinHTTPRequest Files from what site? by DigiP :: "Where would you like to go today?" ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: @echo off cls ECHO [HTTP Downloader :) Full syntax required!!] SET /P file="[example: http://www.google.com/index.html and not http://www.google.com/] : ECHO [What is the file called?] SET /P file2="[example: somefile.html] : type get.txt > get.js echo WScript.Echo(getText("%file%")); >> get.js cscript get.js >> %file2% echo "File Downloaded!" pause del get.js There is a way to script it to download other binary files, like mp3's and exe's but I am still tyring to figure that part out. If anyone wants to help get it to download binary files like exe's, please post what you come up with. (And only using this script setup. I do not want some full C++ program or such to do this, just something simple using this scenario so anyone on a windows machine, xp or later, can make a little script on their own to do the same thing. I am not looking for anything more involved or that requires a compiler. Just a simple, quick and dirty little script for Windows!) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.