tags:

views:

378

answers:

4

Hey! I am trying to connect to my localhost and the index page I have there. But every time I try I just get:

Dados -> HTTP/1.1 400 Bad Request Date: Thu, 16 Apr 2009 15:25:41 GMT Server: Apache/2.2.10 (Win32) PHP/5.2.8 Content-Length: 226 Connection: close Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>400 Bad Request</title> </head><body> <h1>Bad Request</h1> <p>Your browser sent a request that this server could not understand.<br /> </p> </body></html>

Here is the code I have:

#define WIN32_LEAN_AND_MEAN


#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>

int main()
{
    WSADATA WsaDat;

    if(WSAStartup(MAKEWORD(2,0),&WsaDat)!=0)
    {
     printf("Winsock error - Winsock initialization failed\r\n");
     WSACleanup();
     system("PAUSE");
     return 0;
    }

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

    if(Socket==INVALID_SOCKET)
    {
     printf("Winsock error - Socket creation Failed!\r\n");
     WSACleanup();
     system("PAUSE");
     return 0;
    }


    struct hostent *host;
    if((host=gethostbyname("localhost"))==NULL)      
    {
     printf("Failed to resolve hostname.\r\n");
     WSACleanup();
     return 0;
    }

    printf("Resolveu o hostname\r\n");
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port= htons(80);
    SockAddr.sin_family= AF_INET;
    SockAddr.sin_addr.s_addr= *((unsigned long*)host->h_addr);

    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
    {
     printf("Failed to establish connection with server\r\n");
     WSACleanup();
     system("PAUSE");
     return 0;
    }

    char get[] = {
                    "GET / HTTP/1.1\n"
                    "From: bla@ble\n"
                    "User-Agent: HTTPTool/1.0\n"
                    "\n"
                };
    send(Socket,get, strlen(get),0);


    printf("Ligou-se\r\n");
    char buffer[1000];
    int nDataLength = recv(Socket,buffer,1000,0);
    printf("Dados -> %s",buffer);


    shutdown(socket,SD_SEND);


    closesocket(socket);

    WSACleanup();



    return 0;
}

Is there something missing? I am following a tutorial and that's basically the code they have there.

Thanks

+1  A: 

HTTP 1.1 requires a Host: localhost header - either add this or use HTTP 1.0

Also, headers are separated by \r\n, not just \n.

Greg
Thanks! It's working for the localhost but not for other sites (like for this one) Going to try and read a little more about http.
AntonioCS
A: 

As RoBorg said your get string should be:

    char get[] = {
                "GET / HTTP/1.1\r\n"
                "Host: localhost\r\n"
                "From: bla@ble\r\n"
                "User-Agent: HTTPTool/1.0\r\n"
                "\r\n"
            };

If you are connecting to other sites besides localhost the 'host' field will have to be updated (usually hostname, IP address, or www works).

Erik
A: 

It might be easier to try just using HTTP/1.0, which you don't need to specify the extra values:

char get[] = { "GET / HTTP/1.0\r\n" };

Should work everywhere...

Sucuri
A: 

You might also want to try using WinInet for your client-side Http needs instead of just sockets.

Lots of people connect to web services through proxies -- that is, their own computer can't access the web, but it can get to a nearby computer (the proxy) that will forward the request on. You socket code doesn't handle proxies, but the WinInet API will do it for you automatically.

[As an old-fashioned networking person, I loath these devices -- they've essentially badly implemented routers]

Similarly, you might want to switch your server to https: some day; you'll have a heck of a time updating your socket code to handle this, but again, WinInet does it for you.

[Note: I work at Microsoft]

SunriseProgrammer