MFC库参考 -WinInet基础知识

xiaoxiao2021-02-27  145

MFC库参考 - WinInet基础知识 您可以使用WinInet添加FTP支持,从您的应用程序中下载和上传文件。您可以覆盖OnStatusCallback,并在搜索和下载文件时使用dwContext参数向用户提供进度信息。 本文包含以下主题: 创建一个非常简单的浏览器 下载网页 FTP文件 检索Gopher目录 在传输文件时显示进度信息 下面的代码摘录演示如何创建一个简单的浏览器,下载一个网页,FTP一个文件,并搜索一个gopher文件。它们不是完整的示例,并不是全部包含异常处理。 有关WinInet的其他信息,请参阅Win32 Internet Extensions(WinInet)。 创建一个非常简单的浏览器

Create a Very Simple Browser Copy Code #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(); Download a Web Page Copy Code //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 a File Copy Code #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(); Retrieve a Gopher Directory Copy Code #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将使用该Internet会话中所有活动的进度信息调用OnStatusCallback函数。 因为单个会话可能支持多个连接(在其一生中可能执行许多不同的不同操作),OnStatusCallback需要一种机制来识别具有特定连接或事务的每个状态更改。该机制由提供给WinInet支持类中许多成员函数的上下文ID参数提供。此参数始终为DWORD类型,始终命名为dwContext。 分配给特定Internet对象的上下文仅用于标识对象在CInternetSession对象的OnStatusCallback成员中导致的活动。对OnStatusCallback的调用接收到几个参数; 这些参数一起工作,告诉您的应用程序,哪些事务和连接已经取得了哪些进展。 创建CInternetSession对象时,可以为构造函数指定一个dwContext参数。CInternetSession本身不使用上下文ID; 相反,它将上下文ID传递给任何未明确获取自己的上下文ID的InternetConnection派生对象。反过来,如果没有明确指定不同的上下文ID,那么CInternetConnection对象将会将上下文ID传递给他们创建的CInternetFile对象。另一方面,如果您指定自己的特定上下文ID,则该对象及其所做的任何工作将与该上下文ID相关联。您可以使用上下文ID来标识在OnStatusCallback函数中给您的状态信息。 在传输文件时显示进度信息 例如,如果您编写一个与FTP服务器建立连接以读取文件并连接到HTTP服务器以获取网页的应用程序,那么您将拥有一个CInternetSession对象,两个CInternetConnection对象(一个是CFtpSession另一个将是一个CHttpSession)和两个CInternetFile对象(每个连接一个)。如果您使用dwContext参数的默认值,则无法区分表示FTP连接进度的OnStatusCallback调用以及指示HTTP连接进度的调用。如果您指定了一个dwContext ID,您可以稍后在OnStatusCallback中进行测试,您将知道哪个操作生成回调。 也可以看看 概念 MFC互联网编程基础 Win32 Internet Extensions(WinInet) 要提出建议或报告有关帮助或本产品的其他功能的错误,请访问反馈网站。

转载请注明原文地址: https://www.6miu.com/read-16395.html

最新回复(0)