Sign in to follow this  

HTML GET Request method

Recommended Posts

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 -
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
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 "./"
        // 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

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this