使用c++技术实现下载网页

第一种方法:直接使用Socket

第二种方法:WinInet中的类

第三种方法:MFC封装类

下面提供一种实现过程

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <afxinet.h>
 5 using namespace std;
 6 
 7 
 8 int main()
 9 {
10     //创建并且初始化Internet会话
11     CInternetSession session("my brower");
12 
13     //http协议控制对象
14     CHttpConnection* pServer=NULL;
15 
16     //http协议下的文件操作对象
17     CHttpFile* pFile=NULL;
18 
19     //url地址
20     CString m_adress="http://www.baidu.com/"; 
21 
22     CString strServer;
23     CString strObject;
24     INTERNET_PORT nPort;
25     DWORD dwServiceType;
26 
27     //通过url地址解析出url对象的端口
28     AfxParseURL(m_adress,dwServiceType,strServer,strObject,nPort);  //对网址进行解析
29     pServer=session.GetHttpConnection(strServer,nPort);//主要是用来返回一个CHttpConnection的句柄
30     pFile=pServer->OpenRequest(1,strObject,NULL,1,NULL,NULL,INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);
31 
32     pFile->AddRequestHeaders("my brower");    //添加一个或者多个CHttpFile对象到HTTP队列中请求处理
33     pFile->SendRequest();   //把一个请求发送到http服务器ITPUB个人空间
34 
35     CString line;
36     CString strConnect;
37     while(pFile->ReadString(line))
38     {
39         //strConnect+=line+"\r\n";//读取源代码
40         cout << line << endl;// 打印输出百度的源代码
41     }
42 }

 

posted on 2012-09-25 16:13  笔记吧... 可能只有自己看得懂  阅读(1304)  评论(0编辑  收藏  举报