issue with HTML GET Request in C++ program

[b][red]This message was edited by gautam at 2006-9-30 3:13:14[/red][/b][hr]
[b][red]This message was edited by gautam at 2006-9-30 3:12:10[/red][/b][hr]
[b][red]This message was edited by gautam at 2006-9-30 3:11:15[/red][/b][hr]
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 -

[code]
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
[/code]


This is my send request code

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

// hostname has the name as "./www.websitename.com"
// hence do not copy first 2 characters
// the actual code does not have space in between
// HTTP / 1.1 but the messageboard makes it into a link
// HTTP/1.1
sprintf(getReq, "GET /%s HTTP / 1.1
Hostname: %s

",
filename, hostname+2);
int len = strlen(getReq);

while(send(sockfd, getReq, strlen(getReq), 0) < len);
}
[/code]

I can't figure out what is wrong with the code. Is my GET request not framed properly ?

Thanks




Comments

  • : [b][red]This message was edited by gautam at 2006-9-30 3:13:14[/red][/b][hr]
    : [b][red]This message was edited by gautam at 2006-9-30 3:12:10[/red][/b][hr]
    : [b][red]This message was edited by gautam at 2006-9-30 3:11:15[/red][/b][hr]
    : 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 -
    :
    : [code]
    : 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
    : [/code]
    :
    :
    : This is my send request code
    :
    : [code]
    : int SendRequest(int sockfd, const char *hostname, char *filename)
    : {
    : char getReq[4096];
    : memset(getReq, '', 4096);
    :
    : // hostname has the name as "./www.websitename.com"
    : // hence do not copy first 2 characters
    : // the actual code does not have space in between
    : // HTTP / 1.1 but the messageboard makes it into a link
    : // HTTP/1.1
    : sprintf(getReq, "GET /%s HTTP / 1.1
    Hostname: %s

    ",
    : filename, hostname+2);
    : int len = strlen(getReq);
    :
    : while(send(sockfd, getReq, strlen(getReq), 0) < len);
    : }
    : [/code]
    :
    : I can't figure out what is wrong with the code. Is my GET request not framed properly ?
    :
    : Thanks
    :

    I haven`t done socket programming in C, but I believe the solution would be to replace [blue]"
    "[/blue] with [blue]"
    "[/blue] for the newline. That way the line terminator will be CRLF, not LF. Try and see how that works..
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion