Windows Internet编程基础--WinInet

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

  以下摘录的代码说明如何创建一个简单的浏览器、下载 Web 页、FTP 文件和搜索 gopher 文件。它们并不代表完整的示例,并且不都包含异常处理功能。
  
   创建一个非常简单的浏览器
None.gif  #include <afxinet.h>
None.gif   // assumes URL names have been initialized
None.gif
  CInternetSession session("My Session");
None.gif  CStdioFile* pFile = NULL;
None.gif   // use a URL and display a Web page
None.gif
   while (lpszURL = DisplayPage( dot.gif))
ExpandedBlockStart.gif   {
InBlock.gif     pFile = session.OpenURL(lpszURL);
InBlock.gif     while (pFile->Read(szBuff,1024) > 0)
ExpandedSubBlockStart.gif     {
InBlock.gif        //read filedot.gif
ExpandedSubBlockEnd.gif
     }
InBlock.gif     delete pFile;
ExpandedBlockEnd.gif  }
None.gif  session.Close();
None.gif
   下载 Web 页
None.gif   // this code excerpt also demonstrates try/catch exception handling
None.gif
  #include <afxinet.h>
None.gif   // assumes server, port, and URL names have been initialized
None.gif
  CInternetSession session("My Session");
None.gif  CHttpConnection* pServer = NULL;
None.gif  CHttpFile* pFile = NULL;
None.gif   try
ExpandedBlockStart.gif   {
InBlock.gif     CString strServerName;
InBlock.gif     INTERNET_PORT nPort;
InBlock.gif  
InBlock.gif     pServer = session.GetHttpConnection(strServerName, nPort);
InBlock.gif     pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
InBlock.gif     pFile->AddRequestHeaders(szHeaders);
InBlock.gif     pFile->SendRequest();
InBlock.gif     pFile->QueryInfoStatusCode(dwRet);
InBlock.gif  
InBlock.gif     if (dwRet == HTTP_STATUS_OK)
ExpandedSubBlockStart.gif     {
InBlock.gif         UINT nRead = pFile->Read(szBuff, 1023);
InBlock.gif         while (nRead > 0)
ExpandedSubBlockStart.gif         {
InBlock.gif             //read filedot.gif
ExpandedSubBlockEnd.gif
         }
ExpandedSubBlockEnd.gif     }
InBlock.gif     delete pFile;
InBlock.gif     delete pServer;
ExpandedBlockEnd.gif  }
None.gif   catch (CInternetException* pEx)
ExpandedBlockStart.gif   {
InBlock.gif     //catch errors from WinInet
ExpandedBlockEnd.gif
  }
None.gif  session.Close();
None.gif
  FTP 文件
None.gif  #include <afxinet.h>
None.gif   // assumes server and file names have been initialized
None.gif
  CInternetSession session("My FTP Session");
None.gif  CFtpConnection* pConn = NULL;
None.gif  
None.gif  pConn = session.GetFtpConnection(lpszServerName);
None.gif   // get the file
None.gif
   if (!pConn->GetFile(pstrRemoteFile, pstrLocalFile))
None.gif      // display an error
None.gif
  delete pConn;
None.gif  session.Close();
  检索 Gopher 目录
None.gif  #include <afxinet.h>
None.gif   // assumes file name has been initialized
None.gif
  CInternetSession session("My Gopher Session");
None.gif  CGopherConnection* pConn = NULL;
None.gif  CGopherFileFind* pFile;
None.gif  
None.gif  pConn = session.GetGopherConnection("gopher.yoursite.com");
None.gif  pFile =  new CGopherFileFind(pConn);
None.gif  BOOL bFound = pFile->FindFile(lpszFileToFind);
None.gif   while (bFound)
ExpandedBlockStart.gif   {
InBlock.gif     bFound = pFile->FindNextFile();
InBlock.gif     //retrieve attributes of found file
ExpandedBlockEnd.gif
  }
None.gif  delete pFile;
None.gif  delete pConn;
None.gif  session.Close();
None.gif
   使用 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,您将知道是哪个操作生成的回调
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值