Skip to main content
gamedev.net
🔒 Locked

HTML GET Request method

Started by CRACK123 2 replies 1k views
Original Post
CRACK123
CRACK123
Hi, I am trying to generate a GET request in C code by connecting to the site on port 80. However I get a response as -
[Source]
400 - Bad Reqest
Your browser sent a request that this server could not understand.

client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /index.html
[/Source]
This is my send request code

int SendRequest(int sockfd, const char *hostname, char *filename)
{
        char getReq[4096];
        memset(getReq, '\0', 4096);

        // hostname has the name as "./www.websitename.com"
        // hence do not copy first 2 characters
        sprintf(getReq, "GET /%s HTTP/1.1\nHostname: %s\n\n",
                filename, hostname+2);
        int len = strlen(getReq);

        while(send(sockfd, getReq, strlen(getReq), 0) < len);
}

I can't figure out what is wrong with the code. Is my GET request not framed properly ? Thanks
The more applications I write, more I find out how less I know
let_bound
let_bound
Try:

sprintf(getReq, "GET /%s HTTP/1.1\nHost: %s\n\n",                filename, hostname+2);


Genuine question: shouldn't it be \r\n, instead of \n\n? I haven't had to deal with HTTP in a while.

Hope this helps.
Brother Bob
Brother Bob
The error message returned by the server tells you exactly where to look to fix the error. Clicky.
VHL
VHL
Shouldn't it be Host, instead of Hostname?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.