?? ch19.htm
字號(hào):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 19 -- Internet Programming with the WinInet Classes</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch18/ch18.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch20/ch20.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- 19 -</H1>
</CENTER>
<CENTER>
<H1>Internet Programming with the WinInet Classes</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Designing the Internet Query Application</A>
<LI><A HREF="#Heading2">Building the Query Dialog Box</A>
<LI><A HREF="#Heading3">Querying HTTP Sites</A>
<LI><A HREF="#Heading4">Querying FTP Sites</A>
<LI><A HREF="#Heading5">Querying Gopher Sites</A>
<LI><A HREF="#Heading6">Using Gopher to Send a Finger Query</A>
<LI><A HREF="#Heading7">Using Gopher to Send a Whois Query</A>
<LI><A HREF="#Heading8">Future Work</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Designing the Internet Query Application</H2>
<P>Chapter 18, "Sockets, MAPI, and the Internet," introduces the WinInet
classes that you can use to build Internet client applications at a fairly high level.
This chapter develops an Internet application that demonstrates a number of these
classes. The application also serves a useful function: You can use it to learn more
about the Internet presence of a company or organization. You don't need to learn
about sockets or handle the details of Internet protocols to do this.</P>
<P>Imagine that you have someone's email address (<B>kate@gregcons.com, </B>for example)
and you'd like to know more about the domain (<B>gregcons.com </B>in this example).
Perhaps you have a great idea for a domain name and want to know whether it's already
taken. This application, Query, will try connecting to <B>gregcons.com</B> (or <B>greatidea.org</B>,
or any other domain name that you specify) in a variety of ways and will report the
results of those attempts to the user.</P>
<P>This application will have a simple user interface. The only piece of information
that the user needs to supply is the domain name to be queried, and there is no need
to keep this information in a document. You might want a menu item called Query that
brings up a dialog box in which to specify the site name, but a better approach is
to use a dialog-based application and incorporate a Query button into the dialog
box.</P>
<P>A dialog-based application, as discussed in the section "A Dialog-Based Application"
of Chapter 1, "Building Your First Application," has no document and no
menu. The application displays a dialog box at all times; closing the dialog box
closes the application. You build the dialog box for this application like any other,
with Developer Studio.</P>
<P>To build this application's shell, choose File, New from within Developer Studio
and then click the Project tab. Highlight MFC AppWizard(exe), name the application
<B>Query</B>, and in Step 1 choose Dialog Based, as shown in Figure 19.1. Click Next
to move to Step 2 of AppWizard.</P>
<P><A HREF="javascript:popUp('19uvc01.gif')"><B>FIG. 19.1</B></A><B> </B><I>Choose
a dialog-based application for Query.</I></P>
<P>In Step 2 of AppWizard, request an About box, no context-sensitive Help, 3D controls,
no automation or ActiveX control support, and no sockets support. (This application
won't be calling socket functions directly.) Give the application a sensible title
for the dialog box. The AppWizard choices are summarized, as shown in Figure 19.2.
Click Next to move to Step 3 of AppWizard.</P>
<P><A HREF="javascript:popUp('19uvc02.gif')"><B>FIG. 19.2</B></A><B> </B><I>This
application doesn't need Help, automation, ActiveX controls, or sockets.</I></P>
<P>The rest of the AppWizard process will be familiar by now: You want comments,
you want to link to the MFC libraries as a shared DLL, and you don't need to change
any of the classnames suggested by AppWizard. When the AppWizard process is completed,
you're ready to build the heart of the Query application.</P>
<P>
<H2><A NAME="Heading2"></A>Building the Query Dialog Box</H2>
<P>AppWizard produces an empty dialog box for you to start with, as shown in Figure
19.3. To edit this dialog box, switch to the resource view, expand the Query Resources,
expand the Dialogs section, and double-click the IDD_QUERY_DIALOG resource. The following
steps will transform this dialog box into the interface for the Query application.</P>
<P><A HREF="javascript:popUp('19uvc03.gif')"><B>FIG. 19.3</B></A><B> </B><I>AppWizard
generates an empty dialog box for you.</I></P>
<P>
<BLOCKQUOTE>
<P>
<HR>
<strong>TIP:</strong> If working with dialog boxes is still new to you, be sure to read
Chapter 2, "Dialogs and Controls."
<HR>
</BLOCKQUOTE>
<DL>
<DT></DT>
<DD><B>1. </B>Change the caption on the OK button to <B>Query</B>.
<P>
<DT></DT>
<DD><B>2. </B>Change the caption on the Cancel button to <B>Close</B>.
<P>
<DT></DT>
<DD><B>3. </B>Delete the TODO static text.
<P>
<DT></DT>
<DD><B>4. </B>Grab a sizing handle on the right edge of the dialog box and stretch
it so that the dialog box is 300 pixels wide or more. (The size of the currently
selected item is in the lower-right corner of the screen.)
<P>
<DT></DT>
<DD><B>5. </B>At the top of the dialog box, add an edit box with the resource ID
IDC_HOST. Stretch the edit box as wide as possible.
<P>
<DT></DT>
<DD><B>6. </B>Add a static label next to the edit box. Set the text to Site name.
<P>
<DT></DT>
<DD><B>7. </B>Grab a sizing handle along the bottom of the dialog box and stretch
it so that the dialog box is 150 pixels high, or more.
<P>
<DT></DT>
<DD><B>8. </B>Add another edit box and resize it to fill as much of the bottom part
of the dialog box as possible.
<P>
<DT></DT>
<DD><B>9. </B>Give this edit box the resource ID IDC_OUT.
<P>
<DT></DT>
<DD><B>10. </B>Click the Styles tab on the Properties box and select the Multiline,
Horizontal Scroll, Vertical Scroll, Border, and Read-Only check boxes. Make sure
all the other check boxes are deselected.
<P>
</DL>
<P>The finished dialog box and the Style properties of the large edit box will resemble
Figure 19.4.</P>
<P><A HREF="javascript:popUp('19uvc04.gif')"><B>FIG. 19.4</B></A><B> </B><I>Build
the Query user interface as a single dialog box.</I></P>
<P>When the user clicks the Query button, this application should somehow query the
site. The last step in the building of the interface is to connect the Query button
to code with ClassWizard. Follow these steps to make that connection:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Choose View, Class Wizard to bring up ClassWizard.
<P>
<DT></DT>
<DD><B>2. </B>There are three possible classes that could catch the command generated
by the button click, but CQueryDlg is the logical choice because the host name will
be known by that class. Make sure that CQueryDlg is the class selected in the Class
Name drop-down list box.
<P>
<DT></DT>
<DD><B>3.</B> Highlight ID_OK (you did not change the resource ID of the OK button
when you changed the caption) in the left list box and BN_CLICKED in the right list
box.
<P>
<DT></DT>
<DD><B>4. </B>Click Add Function to add a function that will be called when the Query
button is clicked.
<P>
<DT></DT>
<DD><B>5. </B>ClassWizard suggests the name OnOK; change it to <B>OnQuery</B>, as
shown in Figure 19.5, and then click OK.
<P>
</DL>
<P><A HREF="javascript:popUp('19uvc05.gif')"><B>FIG. 19.5</B></A><B> </B><I>Add a
function to handle a click on the Query button, still with the ID IDOK.</I></P>
<P>
<DL>
<DT><I></I></DT>
<DD><B>6. </B>Click the Member Variables tab to prepare to connect the edit controls
on the dialog box to member variables of the dialog class.
<P>
<DT></DT>
<DD><B>7. </B>Highlight IDC_HOST and click Add Variable. As shown in Figure 19.6,
you'll connect this control to a CString member variable of the dialog class m_host.
<P>
<DT></DT>
<DD><B>8. </B>Connect IDC_OUT to m_out, also a CString.
<P>
</DL>
<P>Click OK to close ClassWizard. Now all that remains is to write CQueryDlg::OnQuery(),
which will use the value in m_host to produce lines of output for m_out.</P>
<P><A HREF="javascript:popUp('19uvc06.gif')"><B>FIG. 19.6</B></A><B> </B><I>Connect
IDC_HOST to CQueryDlg::m_host.</I></P>
<H2><A NAME="Heading3"></A>Querying HTTP Sites</H2>
<P>The first kind of connection to try when investigating a domain's Internet presence
is HTTP because so many sites have Web pages. The simplest way to make a connection
using HTTP is to use the WinInet class CInternetSession and call its OpenURL() function.
This will return a file, and you can display the first few lines of the file in m_out.
First, add this line at the beginning of QueryDlg.cpp, after the include of stdafx.h:</P>
<P>
<PRE>#include "afxinet.h"
</PRE>
<P>This gives your code access to the WinInet classes. Because this application will
try a number of URLs, add a function called TryURL() to CQueryDlg. It takes a CString
parameter called URL and returns void. Right-click CQueryDlg in the ClassView and
choose Add Member Function to add TryURL() as a protected member function. The new
function, TryURL(), will be called from CQueryDlg::OnQuery(), as shown in Listing
19.1. Edit OnQuery() to add this code.</P>
<P>
<H4>Listing 19.1  QueryDlg.cpp--CQueryDlg::OnQuery()</H4>
<PRE>void CQueryDlg::OnQuery()
{
const CString http = "http://";
UpdateData(TRUE);
m_out = "";
UpdateData(FALSE);
TryURL(http + m_host);
TryURL(http + "www." + m_host);
</PRE>
<PRE>}
</PRE>
<P>The call to UpdateData(TRUE) fills m_host with the value that the user typed.
The call to UpdateData(FALSE) fills the IDC_OUT read-only edit box with the newly
cleared m_out. Then come two calls to TryURL(). If, for example, the user typed<B>
microsoft.com</B>, the first call would try <B>http://microsoft.com</B> and the second
would try <B>http://www.microsoft.com</B>. TryURL() is shown in Listing 19.2.</P>
<P>
<H4>Listing 19.2  QueryDlg.cpp--CQueryDlg::TryURL()</H4>
<PRE>void CQueryDlg::TryURL(CString URL)
{
CInternetSession session;
m_out += "Trying " + URL + "\r\n";
UpdateData(FALSE);
CInternetFile* file = NULL;
try
{
//We know for sure this is an Internet file,
//so the cast is safe
file = (CInternetFile*) session.OpenURL(URL);
}
catch (CInternetException* pEx)
{
//if anything went wrong, just set file to NULL
file = NULL;
pEx->Delete();
}
if (file)
{
m_out += "Connection established. \r\n";
CString line;
for (int i=0; i < 20 && file->ReadString(line); i++)
{
m_out += line + "\r\n";
}
file->Close();
delete file;
}
else
{
m_out += "No server found there. \r\n";
}
m_out += "------------------------\r\n";
UpdateData(FALSE);
</PRE>
<PRE>}
</PRE>
<P>The remainder of this section presents this code again, a few lines at a time.
First, establish an Internet session by constructing an instance of CInternetSession.
There are a number of parameters to this constructor, but they all have default values
that will be fine for this application. The parameters follow:</P>
<UL>
<LI>LPCTSTR pstrAgent  The name of your application. If NULL, it's filled
in for you, using the name that you gave to AppWizard.
<P>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -