本帖最后由 VisualEleven 于 2011-11-06 12:05:47 编辑

解决方案 »

  1.   

    如果哪位高手不嫌麻烦,可以试着创建个工程运行一下。代码如下。#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;
    }
      

  2.   

    突然发现自己问的有问题,自己程序coockie的值都没有模拟设置,貌似浏览器会在cockie中设置浏览器的信息。可能百度服务器就是根据这个来区别对待请求的
      

  3.   

    需要把对应的cookie也发送过去
      

  4.   

    请求可能还是不一样,检查cookie, useragent.