用WinInet开发Internet客户端应用程序

来源:互联网 发布:js 属性名 编辑:程序博客网 时间:2024/04/30 10:17

WinInet开发Internet客户端应用程序主要步骤:

1 建立Internet会话(Session)--- 创建CInternetSession类对象

MFC通过CInternetSession类对象来实现Internet会话(用这个类不仅可以创建会话,而且可以创建几个并发的Internet会话)

例:CInternetSession session;

2 读写服务器上的文件 --- 创建CInternetFile对象

如果创建的Session要想读写文件,必须创建CInternetFile对象实例(或者是它的子类:CHttpFile或CGopherFile对象实例)。创建CInternetFile对象的方法有两种:

1)调用CInternetSession::OpenURL建立与服务器的连接,返回一个只读的CStdioFile类对象指针(可强制转化为其子类CInternetFile,CHttpFile、CGopherFile类对象指针)

例:CHttpFile pFile=(CHttpFile*)session.OpenURL(“http://www.google.cn/ig/china?hl=zh-CN”);

2)首先调用CInternetSession::GetFtpConnection        创建 CFtpConnection 对象

                 CInternetSession::GetGopherConnection  创建 CGopherConnection 对象

                 CInternetSession::GetHttpConnection       创建 CHttpConnection 对象

    然后调用相应的CFtpConnection::OpenFile

                         CGopherConnection::OpenFile

                         CHttpConnection::OpenRequest

    返回相应的CInternetFile、CGopherFile或者CHttpFile类指针。

例:CHttpConnection *pServer = session.GetHttpConnection(“http://www.google.cn/ig/china?hl=zh-CN”);

pHttpCon->OpenRequest(CHttpConnection::HTTP_VERB_POST);

综上所述,实现Internet客户端应用的步骤因协议而异。要看你是创建基于OpenURL的一般Internet客户端应用,还是使用GetXXXConnection函数之一针对特定协议的Internet客户端应用。(未完待续)