牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

Windows Internet编程基础--WinInet

  可以使用 WinInet 添加 FTP 支持以从应用程序内下载文件和上载文件。可以重写 OnStatusCallback 并使用 dwContext 参数在搜索和下载文件时向用户提供进度信息。
 
  本文包含以下主题:
 
  创建一个非常简单的浏览器
  下载 Web 页
  FTP 文件
  检索 Gopher 目录
  传输文件时显示进度信息 

  以下摘录的代码说明如何创建一个简单的浏览器、下载 Web 页、FTP 文件和搜索 gopher 文件。它们并不代表完整的示例,并且不都包含异常处理功能。
 
  创建一个非常简单的浏览器
  #include <afxinet.h>
  
//assumes URL names have been initialized
  CInternetSession session("My Session");
  CStdioFile
* pFile = NULL;
  
//use a URL and display a Web page
  while (lpszURL = DisplayPage())
  
{
     pFile 
= session.OpenURL(lpszURL);
     
while (pFile->Read(szBuff,1024> 0)
     
{
        
//read file
     }

     delete pFile;
  }

  session.Close();
  下载 Web 页
  //this code excerpt also demonstrates try/catch exception handling
  #include <afxinet.h>
  
//assumes server, port, and URL names have been initialized
  CInternetSession session("My Session");
  CHttpConnection
* pServer = NULL;
  CHttpFile
* pFile = NULL;
  
try
  
{
     CString strServerName;
     INTERNET_PORT nPort;
  
     pServer 
= session.GetHttpConnection(strServerName, nPort);
     pFile 
= pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
     pFile
->AddRequestHeaders(szHeaders);
     pFile
->SendRequest();
     pFile
->QueryInfoStatusCode(dwRet);
  
     
if (dwRet == HTTP_STATUS_OK)
     
{
         UINT nRead 
= pFile->Read(szBuff, 1023);
         
while (nRead > 0)
         
{
             
//read file
         }

     }

     delete pFile;
     delete pServer;
  }

  
catch (CInternetException* pEx)
  
{
     
//catch errors from WinInet
  }

  session.Close();
  FTP 文件
  #include <afxinet.h>
  
//assumes server and file names have been initialized
  CInternetSession session("My FTP Session");
  CFtpConnection
* pConn = NULL;
  
  pConn 
= session.GetFtpConnection(lpszServerName);
  
//get the file
  if (!pConn->GetFile(pstrRemoteFile, pstrLocalFile))
     
//display an error
  delete pConn;
  session.Close();
  检索 Gopher 目录
  #include <afxinet.h>
  
//assumes file name has been initialized
  CInternetSession session("My Gopher Session");
  CGopherConnection
* pConn = NULL;
  CGopherFileFind
* pFile;
  
  pConn 
= session.GetGopherConnection("gopher.yoursite.com");
  pFile 
= new CGopherFileFind(pConn);
  BOOL bFound 
= pFile->FindFile(lpszFileToFind);
  
while (bFound)
  
{
     bFound 
= pFile->FindNextFile();
     
//retrieve attributes of found file
  }

  delete pFile;
  delete pConn;
  session.Close();
  使用 OnStatusCallback
  使用 WinInet 类时,可以使用应用程序的 CInternetSession 对象的 OnStatusCallback 成员来检索状态信息。如果您派生自己的 CInternetSession 对象、重写 OnStatusCallback 并启用状态回调,MFC 将调用 OnStatusCallback 函数并提供那个 Internet 会话中所有活动的进度信息。
 
  由于单个会话可能会支持若干个连接(这些连接在它们的生存期内可能执行许多不同的独特操作),因此 OnStatusCallback 需要一个机制用特定的连接或事务来标识每个状态更改。该机制由分配给 WinInet 支持类中的许多成员函数的上下文 ID 参数提供。该参数的类型总是 DWORD 并且总是命名为 dwContext。
 
  分配给具体某个 Internet 对象的上下文只用于标识此对象在 CInternetSession 对象的 OnStatusCallback 成员中导致的活动。对 OnStatusCallback 的调用将接收几个参数;这些参数共同工作以通知应用程序哪个事务和连接的进度是多少。
 
  当创建 CInternetSession 对象时,可以指定构造函数的 dwContext 参数。CInternetSession 本身不使用上下文 ID,而是将上下文 ID 传递给 InternetConnection 派生的任何对象,这些对象不显式获得它们自己的上下文 ID。反过来,如果您不显式指定不同的上下文 ID,则那些 CInternetConnection 对象将上下文 ID 继续传递给它们创建的 CInternetFile 对象。另一方面,如果您确实指定了自己的特定上下文 ID,对象和它所做的任何工作将与那个上下文 ID 关联。可以使用上下文 ID 来标识 OnStatusCallback 函数中为您提供的状态信息。
 
  传输文件时显示进度信息
  例如,如果编写一个应用程序来创建两个连接,一个连到 FTP 服务器以读取文件,一个连到 HTTP 服务器以获取 Web 页,那么,您将有一个 CInternetSession 对象、两个 CInternetConnection 对象(一个是 CFtpSession,另一个是 CHttpSession)和两个 CInternetFile 对象(分别用于两个连接)。假如对 dwContext 参数使用了默认值,将不能区分指示 FTP 连接进度的 OnStatusCallback 调用和指示 HTTP 连接进度的调用。如果指定以后可在 OnStatusCallback 中测试的 dwContext ID,您将知道是哪个操作生成的回调

posted on 2006-04-14 21:14 杨粼波 阅读(929) 评论(0)  编辑 收藏 引用 所属分类: 文章收藏


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理