对http提出Get请求,获取网页内容

1.MFC的方法

第一种: 


首先要包含这个头文件:#include   <afxinet.h> 

CInternetSession   mySession; 
CHttpFile*   myHttpFile=NULL; 
CString   myData; 
myHttpFile=(CHttpFile*)mySession.OpenURL(url,1,INTERNET_FLAG_DONT_CACHE,NULL,0); 
for(int   i=0;i <20&&myHttpFile-> ReadString(myData);i++) 

strXMLReturn+=myData+ "\r\n ";   //因为每次只能读取一行,所以要循环读取数据 

myHttpFile-> Close(); 
delete   []myHttpFile; 
mySession.Close(); 

CInternetSession::OpenURL函数原型为: 

CStdioFile*   OpenURL(   LPCTSTR   pstrURL,   //文件URL地址 
DWORD   dwContext   =   1,   //上下文ID 
DWORD   dwFlags   =   INTERNET_FLAG_TRANSFER_ASCII,   //标记 
LPCTSTR   pstrHeaders   =   NULL,   //发送到服务器的数据头 
DWORD   dwHeadersLength   =   0   );//发送到服务器的数据头长度 

dwFlags可以为: 
INTERNET_FLAG_RELOAD   强制重读数据 
INTERNET_FLAG_DONT_CACHE   不保存到缓存 
INTERNET_FLAG_TRANSFER_ASCII   使用文本数据 
INTERNET_FLAG_TRANSFER_BINARY   使用二进制数据 

不知道为什么这个函数当只用一个或者两个参数时不会报错,用三个或者按原形就会报错,但是用一个或者两个当页面更新时,不能获取到最新的数据(默认总是先检查缓存是否有要的数据) 

第二种: 

首先: 

#import   <msxml4.dll>   named_guids 
using   namespace   MSXML2; 

CString   resaa; 

IXMLHTTPRequestPtr   httpRes; 
HRESULT   hr=httpRes.CreateInstance( "MSXML2.XMLHTTP "); 
if(!SUCCEEDED(hr)) 

AfxMessageBox( "无法创建XMLHTTP对象,请检查是否安装了MS   XML运行库! "); 


LPCTSTR   url= "http://localhost/changjun/asxml.asp "; 

httpRes-> open( "Get ",url,false, " ", " "); 
httpRes-> send(); 
if((httpRes-> readyState)==4)   //4时表示数据已加载完 

resaa=httpRes-> responseText.copy(); 

httpRes.Release(); 

用这种方式返回值的默认编码方式为UTF-8,所以除了Google中国的网页能正常显示,其他的页面都是乱码,需要将返回值重新编码.


2.纯C++写法:

#include <stdio.h>;
#include <stdlib.h>;
#include <errno.h>;
#include <string.h>;
#include <netdb.h>;
#include <sys/types.h>;
#include <netinet/in.h>;
#include <sys/socket.h>;
#include <stdlib.h>;
#include <netinet/in.h>;
#define MAXDATASIZE 100
#define PORT 2000
int main()
{
        int sockfd,numbytes;
        char buf[MAXDATASIZE];
        struct hostent *he;
        struct sockaddr_in their_addr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
        {
                perror("socked";
                exit(1);
        }
        their_addr.sin_family=AF_INET;
        their_addr.sin_port=htons(PORT);
        their_addr.sin_addr=*((struct in_addr*)he->;h_addr);
        bzero(&(their_addr.sin_zero),;
        if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1)
        {
                perror("connect";
                exit(1);
        }
char *buff="GET www.163.com HTTP/1.0\r\n";
        int writelen;
        int readlen;
        if( ( writelen = write(sockfd,buff,sizeof(sockfd))) != strlen( buff ) )
        {
                fprintf( stderr,"write error\n";

        }
        while ( 1 )
        {
                if( ( readlen = read(sockfd,buff,sizeof(buff))) < 0 )
                {
                                fprintf( stderr,"read error\n";
                                break;
                }
                else {
                        fputs(buff,stdout);
                     }
        close(sockfd);
        return 0;
        }
}
另一种写法:

  1. #include <iostream>;
  2. using namespace std;

  3. #include <sys/types.h>;
  4. #include <sys/stat.h>;
  5. #include <fcntl.h>;
  6. #include <sys/socket.h>;
  7. #include <sys/stat.h>;
  8. #include <unistd.h>;
  9. #include <stdlib.h>;
  10. #include <netdb.h>;
  11. #include <arpa/inet.h>;
  12. #include <netinet/in.h>;
  13. #include <string.h>;
  14. #include <errno.h>;

  15. int main(int argc, char* argv[])
  16. {
  17.         if (argc != 3) {
  18.                 cout << "useage: " << argv[0] << " <ipaddress>;" << " <port>;" << endl;
  19.                 cout << "errno= " << errno << endl;
  20.                 exit(1);
  21.         }

  22.         int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  23.         if (sockfd < 0) {
  24.                 cout << "Can not create socket!" << endl;
  25.                 cout << "errno= " << errno << endl;
  26.                 exit(2);
  27.         }

  28.         struct sockaddr_in cliaddr;
  29.         int len = sizeof(cliaddr);
  30.         memset(&cliaddr, 0, len);
  31.         cliaddr.sin_family = AF_INET;
  32.         cliaddr.sin_addr.s_addr = inet_addr(argv[1]);
  33.         cliaddr.sin_port = htons(atoi(argv[2]));

  34.         if((connect(sockfd, (sockaddr*)&cliaddr, len)) < 0) {
  35.                 cout << "Can not create connect!" << endl;        
  36.                 cout << "errno= " << errno << endl;
  37.                 exit(3);
  38.         }

  39.         char buf[2048];
  40.         strcpy(buf, "GET / HTTP/1.0 \r\n\r\n");
  41.         
  42.         if (write(sockfd, buf, strlen(buf)) < 0) {
  43.                 cout << "Write error!" << endl;
  44.                 cout << "errno= " << endl;
  45.                 exit(4);        
  46.         }

  47.         memset(buf, 0, 2048);
  48.         if (read(sockfd, buf, 2048) < 0) {
  49.                 cout << "Read error!" << endl;
  50.                 cout << "errno= " << endl;
  51.                 exit(5);        
  52.         }

  53.         int ok = open("test.html", O_WRONLY|O_CREAT, 0777);
  54.         if (ok < 0) {
  55.                 cout << "Can not open test.html, please try again!" << endl;
  56.                 cout << "errno= " << errno << endl;
  57.                 exit(6);
  58.         }
  59.         
  60.         if (write(ok, buf, strlen(buf)) < 0) {
  61.                 cout << "Write error!" << endl;
  62.                 cout << "errno= " << endl;
  63.                 exit(7);        
  64.         }

  65.         cout << buf << endl;

  66.         close(sockfd);
  67.         close(ok);
  68. }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值