To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98
Chapter 8 Connecting to the Internet
This chapter covers connecting Visual C++ to the Internet. The Internet hardly needs any introduction, and now Visual C++ includes a great deal of support for connecting your programs to the Internet. Some of it is simpleweve already seen how to use HTML views and download Web pages with themand some not so simple. We take a look at what Visual C++ has to offer in this chapter.
We start by using the HTTP protocol in Visual C++ programs, followed by the FTP protocol. We also use the Web browser control (the control on which HTML views are based), as we build a complete working Web browser.
We begin by using the HTTP protocol to download the Microsoft Visual C++ pages HTML itself.
Putting the HTTP Protocol to Work
In our first program of the chapter, we use the HTTP protocolthe protocol the Web is based onto download the raw HTML of a Web page. In particular, we download the Visual C++ Web page from Microsoft.
------------------------------------------------------
| | |
|------------------------------------------------------ |
| ----------------------------- |
| |<HTML> | |
| |<HEAD> | |
| |<TITLE>Visual Product Start P | |
| |<META NAME=DESCRIPTION CONT | |
| |<META NAME=MS.LOCALE CONTEN | |
| |<META HTTP-EQUIV=Content-Typ | |
| |charset=iso8859-1> | |
| |</HEAD> | |
| ----------------------------- |
| |
------------------------------------------------------
Note that here, were downloading the raw HTML of the Web page and not interpreting it. We learn to interpret it, if we want to do so, later in this chapter when we use the Web browser control.
Creating the HTTP Program
Create a new MDI project named HTTP. We display the HTML of the Visual C++ Web page in our view; to make things easier for ourselves, base the view class on CEditView in Step 6 of AppWizard (allowing us an easy way of displaying multiline text).
To let the user download the HTML of the Visual C++ Web page, add a new item, Download Text, to the File menu and connect a handler function, OnFileDownloadtext(), to that new item now. Our first step is to connect to the Internet.
Starting an Internet Session
You connect to the Internet by creating a new Internet session using the CInternet-Session class. To use that class, you have to include the header file afxinet.h.
// HTTPView.cpp : implementation of the CHTTPView class
//
#include stdafx.h
#include HTTP.h
#include HTTPDoc.h
#include HTTPView.h
#include <afxinet.h> ⇐
Now, in OnFileDownloadtext(), we're free to create a new Internet session,
and in that session, we'll download and display the file we want. That
process starts by creating a new CInternetSession object, which we'll name
Session:
void CHTTPView::OnFileDownloadtext()
{
CInternetSession* Session = new CInternetSession(); ⇐
.
.
.
The CInternetSession class functions appear in Table 8.1.
|
Table 8.1 CInternetSession Functions
|
|
Function
| Does this
|
CInternetSession
| Constructs a CInternetSession object.
|
Close
| Closes the Internet connection when the Internet session is terminated.
|
EnableStatusCallback
| Establishes a status callback routine. EnableStatus-Callback is required for asynchronous operations.
|
GetContext
| Gets the context value for an Internet or application session.
|
GetCookie
| Returns cookies for the specified URL and all its parent URLs.
|
GetCookieLength
| Retrieves the variable specifying the length of the cookie stored in the buffer.
|
GetFtpConnection
| Opens an FTP session with a server. Logs on the user.
|
GetGopherConnection
| Opens a gopher server for an application that is trying to open a connection.
|
GetHttpConnection
| Opens an HTTP server for an application that is trying to open a connection.
|
OnStatusCallback
| Updates the status of an operation when status callback is enabled.
|
OpenURL
| Parses and opens a URL.
|
operator HINTERNET
| Provides a handle to the current Internet session.
|
QueryOption
| Provides possible asserts for error checking.
|
ServiceTypeFromHandle
| Gets the type of service from the Internet handle.
|
SetCookie
| Sets a cookie for the specified URL.
|
SetOption
| Sets options for the Internet session.
|
|
|