⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<HTML> 

<HEAD>

<TITLE>Special Edition Using Visual C++ 5 - Chapter 19</TITLE>

<LINK REL="Next" HREF="ch20.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch20.htm">

<LINK REL="Previous" HREF="ch18.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch18.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000">



<H2><B>Chapter 19</B></H2>

<H2><B>Internet Programming with the WinInet Classes</B></H2>

<hr>

<P><A HREF="index18.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index18.htm" target="text">Chapter 18</A>, &quot;Sockets, MAPI, and the Internet,&quot; introduced 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>

<ul>

<li> <B>An Internet Query Program</B></P>

<P>  The sample application for this chapter investigates Internet domain names such as <B>gregcons.com</B> to see what Internet services are offered.</P>

<li> <B>Query Program Interface</B></P>

<P>  A dialog-based application is a good choice for this application. See how to build one with AppWizard, and how to design and build the dialog box.</P>

<li> <B>World Wide Web Queries</B></P>

<P>  It's simple to write a function that reaches out to a Web site in the domain you are investigating. </P>

<li> <B>FTP Queries</B></P>

<P>  FTP queries are slightly more complex than Web queries, and this section shows how to write them.</P>

<li> <B>Gopher Queries</B></P>

<P>  Gopher is a text-based protocol much like the World Wide Web but older, and with links that are established by system administrators rather than users. This section shows you how to look for a gopher site.</P>

<li> <B>Finger Queries</B></P>

<P>  Finger is an old protocol not directly supported by the WinInet classes, but there is a way that you can use those classes to connect user's email address to real names, learn phone numbers, even get a list of everyone with an account at a given 
domain.</P>

<li> <B>Whois Queries</B></P>

<P>  The Whois database includes contact names and phone numbers for every domain on the Internet. While WinInet doesn't support the protocol, this section shows you a trick that will let you build Whois queries.</P>

</ul>

<H3><B>Designing the Internet Query Application</B></H3>

<P>Imagine that you have someone's e-mail address (<B>kate@gregcons.com, for </B><B>example)</B> and you'd like to know more about the domain (<B>gregcons.com in </B><B>this example)</B>. Or perhaps you have a great idea for a domain name and want to know 
if it's taken already. 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 different ways and will report the result 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 
<U>Q</U>uery that brings up a dialog box in which to specify the name of the site, but a better approach is to use a dialog-based application and incorporate a Query button into the dialog box.</P>

<P>Dialog-based applications, as discussed in the &quot;A Dialog-Based Application&quot; section of <A HREF="index01.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index01.htm" target="text">Chapter 1</A>, &quot;Building Your First Application,&quot; have 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 the shell of this application, choose <U>F</U>ile, <U>N</U>ew from within Developer Studio and then click the Project tab. Highlight MFC AppWizard(exe), name the application <font color="#008000">Query</font>, and in Step 1, choose a 
dialog-based application, as shown in Figure 19.1. Click <U>N</U>ext to move to Step 2 of AppWizard.</P>

<A HREF="Ufigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch19/Ufigs01.gif"><b>Fig. 19.1</b></A>

<P><I>Choose a dialog-based application for Query.</I></P>

<P>In Step 2 of AppWizard, request an About box, no context-sensitive help, 3-D 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 should be as summarized in Figure 19.2. Click <U>N</U>ext to move to Step 3 of AppWizard.</P>

<A HREF="Ufigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch19/Ufigs02.gif"><b>Fig. 19.2</b></A>

<P><I>This application does not need help, Automation, ActiveX controls, </I><I>or sockets.</I></P>

<P>The rest of the AppWizard process should be familiar by now: you want comments, want to link to the MFC libraries as a shared DLL, and don't need to change any of the class names suggested by AppWizard. When the AppWizard process is complete, you are 
ready to build the heart of the Query application.</P>

<H3><B>Building The Query Dialog Box</B></H3>

<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 <font 
color="#008000">IDD_QUERY_DIALOG</font> resource. The following steps will transform this dialog box into the interface for the Query application.</P>

<A HREF="Ufigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch19/Ufigs03.gif"><b>Fig. 19.3</b></A>

<P><I>AppWizard generates an empty dialog box for you.</I></P>

<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">

<P>If working with dialog boxes is still new to you, be sure to read <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, &quot;Dialog Boxes and Controls.&quot;</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<ol> 

<li><P> Change the caption on the OK button to <font color="#008000">Query</font>.</P>

<li><P> Change the caption on the Cancel button to <font color="#008000">Close</font>.</P>

<li><P> Delete the <font color="#008000">TODO</font> static text.</P>

<li><P> 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 at the lower right corner of the screen.)</P>

<li><P> At the top of the dialog box, add an edit box with the resource ID <font color="#008000">IDC_HOST</font>. Stretch the edit box as wide as possible.</P>

<li><P> Add a static label next to the dialog box. Set the text to <font color="#008000">Site </font><font color="#008000">name</font>.</P>

<li><P> Grab a sizing handle along the bottom of the dialog box and stretch it longer, so that dialog box is 150 pixels high, or more.</P>

<li><P> Add another edit box and resize it to fill as much of the bottom part of the dialog box as possible.</P>

<li><P> Give this edit box the resource ID <font color="#008000">IDC_OUT</font>.</P>

<li><P> Click the Styles tab on the Properties box and select the <U>M</U>ulti-line, <U>H</U>orizontal scroll, <U>V</U>ertical scroll, <U>B</U>order, and Read-<U>o</U>nly check boxes. Make sure all the other check boxes are deselected.</P>

</ol>

<P>The finished dialog box, and the Style properties of the large edit box, should resemble Figure 19.4.</P>

<A HREF="Ufigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch19/Ufigs04.gif"><b>Fig. 19.4</b></A>

<P><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>

<ol>

<li><P> Choose <U>V</U>iew, Class <U>W</U>izard to bring up ClassWizard.</P>

<li><P> There are three possible classes that could catch the command generated by the button click, but <font color="#008000">CQueryDlg</font> is the logical choice, because the host name will be known by that class. Make sure that <font 
color="#008000">CQueryDlg</font> is the class selected in the Class name drop-down list box.</P>

<li><P> Highlight <font color="#008000">ID_OK</font> (you did not change the resource ID of the OK button when you changed the caption) in the left list box, and <font color="#008000">BN_CLICKED</font> in the right list box.</P>

<li><P> Click Add Function to add a function that will be called when the Query button is clicked.</P>

<li><P> ClassWizard suggests the name <font color="#008000">OnOK</font>; change it to <font color="#008000">OnQuery</font>, as shown in Figure 19.5, and then click OK.</P>

</ol>

<A HREF="Ufigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch19/Ufigs05.gif"><b>Fig. 19.5</b></A>

<P><I>Add a function to handle a click on the Query button, whose ID is </I><I>still </I><I>IDOK</I><I>.</I></P>

<ol start=6>

<li><P> Click the Member Variables tab to prepare to connect the edit controls on the dialog box to member variables of the dialog class.</P>

<li><P> Highlight <font color="#008000">IDC_HOST</font> and click <U>A</U>dd Variable. As shown in Figure 19.6, you will connect this control to a <font color="#008000">CString</font> member variable of the dialog class called <font 
color="#008000">m_host</font>.</P>

<li><P> Connect <font color="#008000">IDC_OUT</font> to <font color="#008000">m_out</font>, also a <font color="#008000">CString</font>.</P>

</ol>

<A HREF="Ufigs06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch19/Ufigs06.gif"><b>Fig. 19.6</b></A>

<P><I>Connect </I><I>IDC_HOST</I><I> to </I><I>CQueryDlg::m_host</I><I>.</I></P>

<P>Click OK to close ClassWizard. Now all that remains is to write <font color="#008000">CQueryDlg::OnQuery()</font>, which will use the value in <font color="#008000">m_host</font> to produce lines of output for <font color="#008000">m_out</font>.</P>

<H3><B>Querying HTTP Sites</B></H3>

<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 <font 
color="#008000">CInternetSession</font> and call its <font color="#008000">OpenURL()</font> function. This will return a file, and you can display the first few lines of the file in <font color="#008000">m_out</font>. First, add this line at the beginning 
of QueryDlg.cpp:</P>

<pre><font color="#008000">#include &quot;afxinet.h&quot;</font></pre>

<P>This gives your code access to the WinInet classes. Because this application will try a number of URLs, add a function to <font color="#008000">CQueryDlg</font> called <font color="#008000">TryURL()</font>. It takes a <font 
color="#008000">CString</font> parameter called <font color="#008000">URL</font> and returns <font color="#008000">void</font>. Right-click <font color="#008000">CQueryDlg</font> in the ClassView and choose Add Function to add <font 
color="#008000">TryURL()</font> as a protected member function. The new function, <font color="#008000">TryURL()</font>, will be called from <font color="#008000">CQueryDlg::OnQuery()</font> as shown in Listing 19.1. Edit <font 
color="#008000">OnQuery()</font> to add this code.</P>

<P><I>Listing 19.1&#151;QueryDlg.cpp&#151; CQueryDlg::OnQuery()</I></P>

<pre><font color="#008000">void CQueryDlg::OnQuery() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     const CString http = &quot;http://&quot;;</font></pre>

<pre><font color="#008000">     </font></pre>

<pre><font color="#008000">     UpdateData(TRUE);</font></pre>

<pre><font color="#008000">     m_out = &quot;&quot;;</font></pre>

<pre><font color="#008000">     UpdateData(FALSE);</font></pre>

<pre><font color="#008000">     TryURL(http + m_host);</font></pre>

<pre><font color="#008000">     TryURL(http + &quot;www.&quot; + m_host);</font></pre>

<pre><font color="#008000">}</font></pre>

<P>The call to <font color="#008000">UpdateData(TRUE)</font> fills <font color="#008000">m_host</font> with the value that the user typed. The call to <font color="#008000">UpdateData(FALSE)</font> fills the <font color="#008000">IDC_OUT</font> read-only 
edit box with the newly cleared <font color="#008000">m_out</font>. Then come two calls to <font color="#008000">TryURL()</font>. 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>. <font color="#008000">TryURL()</font> is shown in Listing 19.2.</P>

<P><I>Listing 19.2&#151;QueryDlg.cpp&#151;CQueryDlg::TryURL()</I></P>

<pre><font color="#008000">void CQueryDlg::TryURL(CString URL)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     CInternetSession session;</font></pre>

<pre><font color="#008000">     m_out += &quot;Trying &quot; + URL + &quot;\r\n&quot;;</font></pre>

<pre><font color="#008000">     UpdateData(FALSE);</font></pre>

<pre><font color="#008000">     CInternetFile* file = NULL;</font></pre>

<pre><font color="#008000">     try</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          //We know for sure this is an Internet file,</font></pre>

<pre><font color="#008000">          //so the cast is safe</font></pre>

<pre><font color="#008000">          file = (CInternetFile*) session.OpenURL(URL);</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">     catch (CInternetException* pEx)</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          //if anything went wrong, just set file to NULL</font></pre>

<pre><font color="#008000">          file = NULL;</font></pre>

<pre><font color="#008000">          pEx-&gt;Delete();</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">     if (file)</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          m_out += &quot;Connection established. \r\n&quot;;</font></pre>

<pre><font color="#008000">          CString line;</font></pre>

<pre><font color="#008000">          for (int i=0; i &lt; 20 &amp;&amp; file-&gt;ReadString(line); i++)</font></pre>

<pre><font color="#008000">          {</font></pre>

<pre><font color="#008000">               m_out += line + &quot;\r\n&quot;;</font></pre>

<pre><font color="#008000">          }</font></pre>

<pre><font color="#008000">          file-&gt;Close();</font></pre>

<pre><font color="#008000">          delete file;</font></pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -