资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
开发Internet应用程序Internet应用程序开发的几种类型:1、使用WinInet类开发Internet应用程序:WinInet类支持HTTP、FTP和Gopher等标准的协议。2、使用Windows Socket开发Internet应用程序:Winsock标准定义了一个DLL接口来连接Internet,MFC使用CAsyncSocket和CSocket类对接口进行了封装。3、使用消息收发API(MAPI:Message API) 开发Internet应用程序:使用MAPI可以很方便的向其他应用程序发送电子邮件、语音邮件或传真等功能。WinInet类是一个总称,目前的版本中分为四组CInternetSession类CInternetConnection类(连接类)CFileFind类(Internet文件查找类)CInternetFile类和CGopherLocator类 (1)CInternetSession类直接继承自CObject类,该类用来建立与某个Internet服务器的会话。还可以向代理服务器描述连接,如果应用程序所使用的Internet连接必须保持一段时间,则可以在CWinApp类中创建相应的CInternetSession成员。(2)CInternetConnection类及其派生类CHttpConnection、CFtpConnection和CGopherConnection类,这些类帮助用户管理与Internet服务器的连接,同时还提供一些函数完成和响应服务器的通信:CInternetConnection:用于管理与Internet服务器的连接;CFtpConnection:用于管理与FTP服务器的连接,可以对服务器上的文件和目录进行直接操作;CGopherConnection:管理与Gopher服务器的连接;CHttpConnection:管理与HTTP服务器的连接。(3)CInternetFile类及其派生类CHttpFile、CGopherFile。这些类实现对远程系统上的文件的存取工作。文件类还包含CFileFind类及其派生类CFtpFileFind、CGopherFileFind类。CFileFind类直接继承于CObject类,这些类实现对本地和远程系统上的文件的搜索和定位工作。CInternetFile:允许对使用Internet协议的远程系统中的文件进行操作CGopherFile:为在Gopher服务器上进行文件检索和读取操作提供支持CHttpFile:提供对HTTP服务器上的文件进行操作的支持CFindFile:文文件检索提供支持CFtpFileFind:为在FTP服务器上进行的文件检索操作提供支持CGopherFileFind:为在Gopher服务器上进行的文件检索操作提供支持(4)在从Gopher服务器中获取信息之前,必须先获得该服务器的定位器,而CGopherLocator类的主要功能就是从Gopher服务器中得到定位并确定定位器的类型。WinInet类编程实例 在新加入的类中增加如下三个public的成员函数,在头文件MyWinInetClass.h中可看到这些函数。CString ConnectFtp(const CString sUrl);/完成连接Ftp功能的函数CString ConnectHttp(const CString sUrl)/完成连接Http功能的函数CString ConnectGopher(const CString sUrl);为了建立Internet的会话,新增加的CmyWinInetClass类中加入一个private型成员变量m_session:CInternetSession m_session;/建立Internet会话由于在上面定义了一个CWinInet类的对象,所以还需要在MyWinInetClass.h头文件中加入如下代码:#include afxinet.h#include #pragma comment(lib, wininet.lib)为CMyWinInetClass类添加三个用于连接的成员函数,它们分别是ConnectFtp、ConnectHttp和ConnectGopher。CString CMyWinInetClass:ConnectFtp(const CString sUrl)CString sResult;/存储连接信息的字符串CFtpConnection *Ftpconnection = NULL;sResult = sResult + Trying to connect Ftp sites + sUrl + rn;Ftpconnection = m_session.GetFtpConnection(sUrl);/建立到Ftp服务器的连接if(Ftpconnection)sResult = sResult + Connection established.rn;CString sCurDir;Ftpconnection-GetCurrentDirectory(sCurDir);/得到Ftp服务器的当前目录sResult = sResult + current directory is + sCurDir + rn;Ftpconnection-Close();/关闭连接else sResult=sResult+There are some errors in finding this Ftp sites;return sResult; CString CMyWinInetClass:ConnectHttp(const CString sUrl)CString sResult;CInternetFile *hHttpFile = NULL;sResult = sResult + Trying to connect Http sites + sUrl + rn;hHttpFile = (CInternetFile *)m_session.OpenURL(sUrl);/得到文件指针if(hHttpFile)sResult = sResult + Connection established.rn;CString sLine;while(hHttpFile-ReadString(sLine)/读取Http服务器上的内容sResult = sResult + sLine + rn;hHttpFile-Close(); /关闭连接elsesResult = sResult + There are some errors in finding this Http sites;return sResult; CString CMyWinInetClass:ConnectGopher(const CString sUrl)CString sResult;CInternetFile *hGopherFile=NULL;sResult = sResult + Trying to connect Gopher sites + sUrl + rn;hGopherFile = (CInternetFile *)m_session.OpenURL(sUrl);/得到文件指针if(hGopherFile)sResult = sResult + Connection established.rn;CString sLine;while(hGopherFile-ReadString(sLine)/读取Gopher服务器内容sResult = sResult+sLine+rn;hGopherFile-Close();/结束连接elsesResult = sResult + There are some errors in finding this Gopher sites;return sResult;在CMyInternetDlg类中增加一个public成员变量CMyWinInetClass m_WinInetClass;变量m_WinInetClass是CMyWinInetClass类的一个对象,所以还要在WinInetDlg.h头文件加入自定义类的头文件:#include MyWinInetClass.h三个消息处理函数增加代码如下:void CWinInetDlg:OnButtonFtp() UpdateData(TRUE);/从对话框读入地址信息m_Urlm_EditResult=;/调用自定义类的成员函数,连接Ftp服务器,m_Url为地址m_EditResult = m_EditResult + m_WinInetClass.ConnectFtp(m_Url); UpdateData(FALSE);void CWinInetDlg:OnButtonHttp() UpdateData(TRUE);/从对话框读入地址信息m_Urlm_EditResult=;/调用自定义类的成员函数,连接Http服务器,m_Url为地址m_EditResult = m_EditResult + m_WinInetClass.ConnectHttp(m_Url);UpdateData(FALSE);void CWinInetDlg:OnButtonGopher() UpdateData(TRUE);/从对话框读入地址信息m_Urlm_EditResult=;/调用自定义类的成员函数,连接Gopher服务器,m_Url为地址m_EditResult = m_EditResult + m_WinInetClass.ConnectGopher(m_Url);UpdateData(FALSE);总结:只是实现了读取服务器的内容,相当于网页的代码。FTP怎样实现呢?以及Gopher是什么
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号