The Command-line HTTP Client Tool
In this issue, Ken Spencer introduces you to the Microsoft Windows 2000 Resource Kit utility Command-line HTTP Client.
November 21, 2000
The Windows 2000 Resource Kit
If you need to access a Web site from the command line or a batch file, the Command-line HTTP Client tool (httpcmd.exe) can help. This Microsoft Windows 2000 Resource Kit utility reads its input commands from simple text files, many of which are generic so that you can use them with several Web sites.
Occasionally, you might require a tool that can access a Web site or application from the command line. For example, you might want to access a site from a batch file or test to see whether a site is running. You might also need to copy the HTML content from a Web page and save it to disk. If you perform tasks such as these repeatedly, consider httpcmd.exe.
Creating and Modifying Input Files
Before you can use httpcmd.exe, you need to create an input (.req) file. Input files contain the HTTP commands you use to access a Web site. You must specify the Web site or server address on the command line.
You can create simple or complex input files. For example, I copied the contents of the default.req file
GET /default.htm HTTP/1.0
(which is installed with the resource kit) to getdefault.req. To use this input file with the HTTP command, I opened a command prompt and issued the command
httpcmd www.32x.com getdefault.req
This command executes the HTTP command and accesses default.htm from my Web site—http://www.32x.com. I could access default.htm from any other Web site by reissuing the command with a new Web site address or server.
After the command executes, it displays the returned HTML or HTTP stream to the command prompt display (STDOUT). To save this output, use the Redirect option with the command
httpcmd www.32x.com getdefault.req > 32xresults.htm
This command sends the output to the 32xresults.htm file. If you open 32xresults.htm in a browser, it displays the default.htm page from the Web site. In this way, you can use the Command-line HTTP Client tool to retrieve a Web page and save it to disk.
The syntax of the HTTP command includes several options, as Table 1 shows. For example, use the input file
//// Tests If-Modified-Since headers//GET /default.htm HTTP/1.0If-Modified-Since: Sun, 17 Sep 2000 08:00:00 GMT
to retrieve a page only if it's been modified after a given date. When you execute the HTTP command with this file, a page will appear only if it's been modified after Sunday, September 17, 2000, at 8:00 a.m. Greenwich Mean Time (GMT). If you use this command against http://www.32x.com, you'll receive the display that Figure 1, page 16, shows. In the first line, error 304 Not modified shows that no one has modified the page since the request date. The second line shows the type of server the site uses. The third line shows the date and time of the request. The fourth line shows the ETag, which is an HTTP header tag, and the fifth line shows the content length as 0. The content length is 0 because the file wasn't modified between September 17, 2000, and the request date in the input file.
Next, change the input file contents to
GET /default.htm HTTP/1.0If-Modified-Since: Sun, 4 June 2000 08:00:00 GMT
and save the input file to a new file. When you run the command with the new input file, you receive the display that Figure 2 shows, followed by the HTML content. The new lines in the output indicate that the request was successful. Content-Type is now text/html, the Accept-Ranges setting shows bytes, and Content-Length now shows the length of the HTML content returned.
The folder in which you install the resource kit (by default, C:program filesresource kit) contains several sample .req files. To better understand what you can do with the HTTP command, run these .req files against a familiar Web site, such as http://www.microsoft.com. Then, you can change entries, such as the file date, in the input file and see the results. Some of the sample .req files require input or executable files and won't work unless you customize them for your files. Most of the sample .req files require tweaking to be useful.
Next Month
You can use the Command-line HTTP Client tool for many tasks, such as automating data retrieval from a site or service or testing services. You can also use the command in a batch file to test a site under development. Copy the sample .req files to a new folder, and experiment with them. Make changes to the files and test them, but be careful of syntax when you make your changes. For example, checking for a file-modified date works only if you format your date correctly (see ifmod.req for examples of date requests).
Next month, I'll examine the HTTP Monitoring tool. With that utility, you can monitor Web sites and report the results to a file or the event log.
About the Author
You May Also Like