Jump to content
  • Advertisement
Sign in to follow this  

HTML GET Request method

This topic is 4377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!