Reading a web page is one way to say "Hello World" to the Internet.
Microsoft's recently released ActiveX toolkit includes the Microsoft Internet SDK (a.k.a inetsdk). The WININET Win32 toolkit provides a convenient API to internet protocols, including Gopher, FTP, and HTTP, without the need to include any Windows sockets code or TCP/IP management code. In this article, I briefly describe how to use the Win32 Internet API to open a simple HTTP connection and retrieve a web page.
Retrieving a Web Page
The Win32 Internet functions enable your application to easily retrieve pages from an Internet server. After including the right headers and libraries in your project, you can add a class to do HTTP retrievals. Table 1 lists some of the basic Internet HTTP functions available. I've written a simple class using these functions to handle all of the internet communication (Listing 1) .
CInternetClass's constructor takes care of all of the initialization required for the Win32 Internet functions. CInternetClass::GetUrl takes a complete URL passed in as a string, extracts the server and path, and does all that is necessary to retrieve the page. It returns the text in a buffer passed in by the caller. GetUrl calls the Win32 Internet function InternetOpenUrl to open the specified URL. InternetOpenUrl does all the necessary string parsing to find the server, connect to the server, and open the correct kind of connection. You can use the InternetConnect and HttpOpenRequest functions to accomplish the same thing, but using InternetOpenUrl simplifies the process greatly. InternetReadFile is the function that actually reads the data in any mode of operation. Finally, InternetCloseHandle is used to close the connection. The amount of code is trivial, and advanced functions such as proxy server support are transparent except for the setup.
Installation and Support
Microsoft currently ships The Win32 Internet functions as part of the ActiveX toolkit. The Internet functions are installed by Microsoft Internet Explorer, or through a separate install package. Microsoft will be including these functions as part of future Windows operating systems. You can find more information about the ActiveX toolkit at http://www.microsoft.com/activeplatform/default.ASP
Conclusion
The Microsoft Internet SDK provides a convenient and easy way to integrate your applications with the Internet, without requiring lots of time writing low-level Windows Sockets code or TCP/IP transactions. By leveraging Microsoft's API, you can avoid rewriting and debugging code, and easily link your application to the Internet. o
Benjamin Kuo is a Software Engineer at Paracel, Inc. in Pasadena, CA. He is currently developing low-level firmware and software, as well as Windows application code for an Internet news filtering service (Paracel Today http://www.paracel.com). He may be reached at kuo@paracel.com..