您的位置:首页 > 理论基础 > 计算机网络

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

2011-11-18 16:23 621 查看
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;

        }

}
另一种写法:

#include <iostream>;

using namespace std;

#include <sys/types.h>;

#include <sys/stat.h>;

#include <fcntl.h>;

#include <sys/socket.h>;

#include <sys/stat.h>;

#include <unistd.h>;

#include <stdlib.h>;

#include <netdb.h>;

#include <arpa/inet.h>;

#include <netinet/in.h>;

#include <string.h>;

#include <errno.h>;

int main(int argc, char* argv[])

{

        if (argc != 3) {

                cout << "useage: " << argv[0] << " <ipaddress>;" << " <port>;" << endl;

                cout << "errno= " << errno << endl;

                exit(1);

        }

        int sockfd = socket(AF_INET, SOCK_STREAM, 0);

        if (sockfd < 0) {

                cout << "Can not create socket!" << endl;

                cout << "errno= " << errno << endl;

                exit(2);

        }

        struct sockaddr_in cliaddr;

        int len = sizeof(cliaddr);

        memset(&cliaddr, 0, len);

        cliaddr.sin_family = AF_INET;

        cliaddr.sin_addr.s_addr = inet_addr(argv[1]);

        cliaddr.sin_port = htons(atoi(argv[2]));

        if((connect(sockfd, (sockaddr*)&cliaddr, len)) < 0) {

                cout << "Can not create connect!" << endl;        

                cout << "errno= " << errno << endl;

                exit(3);

        }

        char buf[2048];

        strcpy(buf, "GET / HTTP/1.0 \r\n\r\n");

        

        if (write(sockfd, buf, strlen(buf)) < 0) {

                cout << "Write error!" << endl;

                cout << "errno= " << endl;

                exit(4);        

        }

        memset(buf, 0, 2048);

        if (read(sockfd, buf, 2048) < 0) {

                cout << "Read error!" << endl;

                cout << "errno= " << endl;

                exit(5);        

        }

        int ok = open("test.html", O_WRONLY|O_CREAT, 0777);

        if (ok < 0) {

                cout << "Can not open test.html, please try again!" << endl;

                cout << "errno= " << errno << endl;

                exit(6);

        }

        

        if (write(ok, buf, strlen(buf)) < 0) {

                cout << "Write error!" << endl;

                cout << "errno= " << endl;

                exit(7);        

        }

        cout << buf << endl;

        close(sockfd);

        close(ok);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息