获取的网页和浏览器获取的网页内容不一样? 本帖最后由 VisualEleven 于 2011-11-06 12:05:47 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果哪位高手不嫌麻烦,可以试着创建个工程运行一下。代码如下。#include <string>#include <iostream>#include <fstream>#include "winsock2.h"#include <time.h>#pragma comment(lib, "ws2_32.lib") using namespace std;#define DEFAULT_PAGE_BUF_SIZE 1048576void main(){ WSADATA wsaData; int err; err = WSAStartup(MAKEWORD(2,2), &wsaData); if( err != 0 ) { return; } // timer is start /s/comment_441b6f800102dy3y_1.html clock_t start, finish; double duration; start = clock(); char host[] = "www.baidu.com"; char *request = "GET / HTTP/1.1\r\nAccept: */*\r\nAccept-Language: zh-cn\r\nHost: www.baidu.com\r\nConnection: Close\r\n\r\n"; struct hostent *hp; hp = gethostbyname(host); if(hp == NULL) { cout << "gethostbyname() error in GetIpByHost: " << host << endl; return; } // 获取域名对应的IP struct in_addr inAddr; LPSTR lpAddr; lpAddr = hp->h_addr; memmove(&inAddr,lpAddr,4); int sock, ret = 0, optval = 1; struct sockaddr_in sa; sa.sin_family = AF_INET; sa.sin_port = htons(80); sa.sin_addr.s_addr = inet_addr(inet_ntoa(inAddr)); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); connect(sock, (SOCKADDR*)&sa, sizeof(sa)); if(sock == -1) { return; } if(sock == -2) { return; } // send the "GET" data ret = send(sock, request, strlen(request), 0); // 网页内容长度。可以从http头部数据中获取 "Content-Length:" int m_nContentLength = DEFAULT_PAGE_BUF_SIZE; char *pageBuf; pageBuf = (char *)malloc(m_nContentLength); memset(pageBuf, 0, m_nContentLength); int bytesRead = 0; while(ret > 0) { ret = recv(sock, pageBuf + bytesRead, m_nContentLength - bytesRead, 0); if(ret > 0) { bytesRead += ret; } } pageBuf[bytesRead] = '\0'; cout << bytesRead << endl; // write the html content to the file ofstream ofs; ofs.open("ofs.txt"); ofs << pageBuf<< endl; ofs.close(); //string s(pageBuf,1048) ; //cout<<s<<endl; free(pageBuf); closesocket(sock); WSACleanup(); // timer is finish finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; cout << "have cost " << duration << " seconds\n"; return;} 突然发现自己问的有问题,自己程序coockie的值都没有模拟设置,貌似浏览器会在cockie中设置浏览器的信息。可能百度服务器就是根据这个来区别对待请求的 需要把对应的cookie也发送过去 请求可能还是不一样,检查cookie, useragent. 请教个基础然而重要函数返回值问题 关于live555库菜鸟问题 新手:请指教一下学习的方向 一个简单的关于数据库查询的问题 使用WINDOWS标准保存对话框,怎样把软驱和光驱选择去掉?谢谢. 人发霉了!为什么我死机后,只有一个 CPP 文件打不开,别的都能打开,最主要的一个文件啊 VC+ADO数据库编程中遇到的问题! 在我调试程序时,出现了有关"cs3250mt.dll"文件的出错信息,是怎么回事呢,急死我了!!!大家帮帮我吧!!! 怎样派生一个在classwizard没有列出的类? TCP通讯时 接收和发送完全不受影响吗?? 中文字符串如何转为char* 实现双线程请教!
#include <iostream>
#include <fstream>
#include "winsock2.h"
#include <time.h>#pragma comment(lib, "ws2_32.lib") using namespace std;#define DEFAULT_PAGE_BUF_SIZE 1048576void main()
{
WSADATA wsaData;
int err;
err = WSAStartup(MAKEWORD(2,2), &wsaData);
if( err != 0 )
{
return;
} // timer is start /s/comment_441b6f800102dy3y_1.html clock_t start, finish;
double duration;
start = clock(); char host[] = "www.baidu.com";
char *request = "GET / HTTP/1.1\r\nAccept: */*\r\nAccept-Language: zh-cn\r\nHost: www.baidu.com\r\nConnection: Close\r\n\r\n"; struct hostent *hp;
hp = gethostbyname(host);
if(hp == NULL)
{
cout << "gethostbyname() error in GetIpByHost: " << host << endl;
return;
}
// 获取域名对应的IP struct in_addr inAddr;
LPSTR lpAddr;
lpAddr = hp->h_addr;
memmove(&inAddr,lpAddr,4); int sock, ret = 0, optval = 1;
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(80);
sa.sin_addr.s_addr = inet_addr(inet_ntoa(inAddr));
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connect(sock, (SOCKADDR*)&sa, sizeof(sa));
if(sock == -1)
{
return;
}
if(sock == -2)
{
return;
} // send the "GET" data ret = send(sock, request, strlen(request), 0); // 网页内容长度。可以从http头部数据中获取 "Content-Length:" int m_nContentLength = DEFAULT_PAGE_BUF_SIZE;
char *pageBuf;
pageBuf = (char *)malloc(m_nContentLength);
memset(pageBuf, 0, m_nContentLength); int bytesRead = 0;
while(ret > 0)
{
ret = recv(sock, pageBuf + bytesRead, m_nContentLength - bytesRead, 0);
if(ret > 0)
{
bytesRead += ret;
}
}
pageBuf[bytesRead] = '\0';
cout << bytesRead << endl; // write the html content to the file ofstream ofs;
ofs.open("ofs.txt");
ofs << pageBuf<< endl; ofs.close();
//string s(pageBuf,1048) ;
//cout<<s<<endl;
free(pageBuf);
closesocket(sock);
WSACleanup(); // timer is finish finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "have cost " << duration << " seconds\n";
return;
}