Pat
Pat

Reputation: 649

Get website source

So I have this source which gets the source from a website:

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();

The problem is, its not navigating to auth.php. Also, how would I send GET vars like ?name=bleh

Upvotes: 2

Views: 614

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993085

You're sending:

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

Instead, it sounds like you want:

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

Note that the Host: header should contain the domain name only, not the URL.

Upvotes: 3

Related Questions