获取网站来源

时间:2011-12-07 05:07:43

标签: c++

所以我有这个来自网站的来源:

WSADATA wsaData;

if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
            cout << "WSAStartup failed.\n";
    system("pause");
            return 1;
}

    SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    struct hostent *host;
    host = gethostbyname("www.site.com");

    SOCKADDR_IN SockAddr;
    SockAddr.sin_port=htons(80);
    SockAddr.sin_family=AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

    cout << "Connecting...\n";
    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
            cout << "Could not connect";
            system("pause");
            return 1;
    }
    cout << "Connected.\n";

    send(Socket,"GET / HTTP/1.1\r\nHost: www.site.com/auth.php\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.site.com/auth.php\r\nConnection: close\r\n\r\n"),0);
    char buffer[10000];

    int nDataLength;
    while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){               
            int i = 0;
            for(; i != nDataLength; ++i)
                    cout << buffer[i];
    }

    closesocket(Socket);
    WSACleanup();

问题是,它没有导航到auth.php。另外,我如何发送GET变量,如?name = bleh

1 个答案:

答案 0 :(得分:3)

您正在发送:

GET / HTTP/1.1
Host: www.site.com/auth.php
Connection: close

相反,它听起来像你想要的那样:

GET /auth.php HTTP/1.1
Host: www.site.com
Connection: close

请注意,Host:标题应包含域名 only ,而不是URL。