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

📄 ch19.htm

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

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

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

<pre><font color="#008000">     if (connection)</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">          CGopherLocator locator = connection-&gt;CreateLocator(NULL, NULL, </font><font color="#008000">GOPHER_TYPE_TEXT_FILE);</font></pre>

<pre><font color="#008000">          CGopherFile* file = connection-&gt;OpenFile(locator);</font></pre>

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

<pre><font color="#008000">          {</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>

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

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

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

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

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

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

<pre><font color="#008000">          m_out += &quot;No server found there. \r\n&quot;;</font></pre>

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

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

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

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

<P>Add a line at the end of <font color="#008000">OnQuery()</font> that calls this new function:</P>

<pre><font color="#008000">     TryFinger(m_host);</font></pre>

<P>Now, build and run the application. Figure 19.11 shows the result of a query on the site <B>whitehouse.gov</B>, scrolled down to the Finger section.</P>

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

<P><I>Query gets e-mail addresses from the White House Finger server.</I></P>

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

<P>If the site you are investigating is not running a Finger server, there will be a longer than usual delay, and a message box will appear telling you the connection timed out. Click OK on the message box if it appears.</P>

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

<H3><B>Using Gopher to send a Whois Query</B></H3>

<P>One last protocol provides information about sites. It, too, is an old protocol not supported directly by the WinInet classes. It is called Whois, and it's a service offered by only a few servers on the whole Internet. The servers that offer this 
service are maintained by the organizations that register domain names. For example, domain names that end in <B>.com</B> are registered through an organization called InterNIC, and it runs a Whois server called <B>rs.internic.net</B> (the <I>rs</I> stands 
for Registration Services.) Like Finger, Whois responds to a string sent on its own port; the Whois port is 43. Unlike Finger, you don't send an empty string in the locator; you send the name of the host that you want to look up. You connect to 
<B>rs.internic.net</B> every time. (Dedicated Whois servers offer users a chance to change this, but in practice, no one ever does.)</P>

<P>Add a function called <font color="#008000">TryWhois()</font>; as usual, it takes a <font color="#008000">CString host</font> and returns <font color="#008000">void</font>. The code is in Listing 19.9.</P>

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

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

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

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

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

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

<pre><font color="#008000">     CGopherConnection* connection = NULL;</font></pre>

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

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

<pre><font color="#008000">          connection = session.GetGopherConnection(&quot;rs.internic.net&quot;,NULL,NULL,43);</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 connection to NULL</font></pre>

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

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

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

<pre><font color="#008000">     if (connection)</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">          CGopherLocator locator = connection-&gt;CreateLocator(NULL, host, </font><font color="#008000">GOPHER_TYPE_TEXT_FILE);</font></pre>

<pre><font color="#008000">          CGopherFile* file = connection-&gt;OpenFile(locator);</font></pre>

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

<pre><font color="#008000">          {</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>

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

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

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

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

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

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

<pre><font color="#008000">          m_out += &quot;No server found there. \r\n&quot;;</font></pre>

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

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

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

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

<P>Add a line at the end of <font color="#008000">OnQuery()</font> to call it:</P>

<pre><font color="#008000">     TryWhois(m_host);</font></pre>

<P>Build and run the application one last time. Figure 19.12 shows the Whois part of the report for <B>mcp.com</B>&#151;this is the domain for Macmillan Computer Publishing, Que's parent company.</P>

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

<P><I>Query gets real-life addresses and names from the InterNIC Whois </I><I>server.</I></P>

<P>Adding code after the Finger portion of this application means that you can no longer ignore the times when the Finger code cannot connect. When the call to <font color="#008000">OpenFile()</font> in <font color="#008000">TryFinger()</font> tries to 
open a file on a host that is not running a Finger server, an exception is thrown. Control will not return to <font color="#008000">OnQuery()</font>, and <font color="#008000">TryWhois()</font> will never be called. To prevent this, you must wrap the call 
to <font color="#008000">OpenFile()</font> in a try and catch block. Listing 19.10 shows the changes to make.</P>

<P><I>Listing 19.10&#151;QueryDlg.cpp&#151;Changes to TryFinger()</I></P>

<pre><font color="#008000">//replace this line:</font></pre>

<pre><font color="#008000">          CGopherFile* file = connection-&gt;OpenFile(locator);</font></pre>

<pre><font color="#008000">//with these lines:</font></pre>

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

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

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

<pre><font color="#008000">               file = connection-&gt;OpenFile(locator);</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>

<P>Change <font color="#008000">TryFinger()</font>, build Query again, and query a site that does not run a Finger server, such as <B>microsoft.com</B>. You should successfully reach the Whois portion of the application.</P>

<H3><B>Future Work</B></H3>

<P>The Query application built in this chapter does a lot, but it could do much more. There are e-mail and news protocols that could be reached by stretching the WinInet classes a little more and using them to connect to the standard ports for these other 
services. You could also connect to some well-known Web search engines and submit queries by forming URLs according to the pattern used by those engines. In this way, you can automate the sort of poking around on the Internet that most of us do when we're 
curious about a domain name or an organization.</P>

<P>If you'd like to learn more about Internet protocols, port numbers, and what's happening when a client connects to a server, you might want to read Que's <I>Building Internet Applications with Visual C++.</I> The book was written for Visual C++ 2.0, 
and though all the applications in the book compile and run under later versions of MFC, they would be much shorter and easier to write now. Still, the insight into the way the protocols work is valuable.</P>

<P>The WinInet classes can do much more than you've seen here, too. Query doesn't use them to retrieve real files over the Internet. There are two WinInet sample applications included with Visual C++ 5.0 that do a fine job of showing how to retrieve 
files:</P>

<ul>

<li> FTPTREE builds a tree list of the files and directories on an FTP site.</P>

<li> TEAR brings back a page of HTML from a Web site.</P>

</ul>

<P>There are a lot more Microsoft announcements coming over the next few months, as well. Keep an eye on its Web site, <A HREF="tppmsgs/msgs0.htm#26" tppabs="http://www.microsoft.com/" target="_top"><B>www.microsoft.com</B></A>, for libraries and software development kits that will 
make Internet software development even easier and faster.</P>

<H3><B>From Here...</B></H3>

<P>This chapter introduced you to the WinInet classes as one way to write Internet programs. Some related chapters that may interest you include the following:</P>

<ul>

<li> <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; compares dialog-based applications to the MDI or SDI applications that AppWizard can also generate for you.</P>

<li> <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; introduces you to the concepts involved in Internet programming.</P>

<P> </P>

<li> <A HREF="index20.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index20.htm" target="text">Chapter 20</A>, &quot;Building an Internet ActiveX Control,&quot; discusses small efficient controls designed to run quickly and smoothly in an Internet Web page.</P>

</ul>

<p><hr></p>

<center>

<p><font size=-2>

&copy; 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a

Simon and Schuster Company.</font></p>

</center>

</BODY></HTML>

⌨️ 快捷键说明

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