Thread: sending HTTP POST data with Socket

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    sending HTTP POST data with Socket

    Hi Im trying to send a HTTP POST Request data content to a server using C socket
    Everything seemed to work fine except this part

    fprintf(http->file, "POST http://some url with POST form in php HTTP/1.1\r\n");
    fprintf(http->file, "Host: hostname\r\n");
    fprintf(http->file, "Content-Type: applicationx-www-form-urlencoded\r\n");
    fprintf(http->file, "Content-Length: 10\r\n\r");
    fprintf(http->file, "name=kkkkk\r\n");
    fprintf(http->file, "\r\n");
    http->file is file pointer to an open socket



    the page on the server looks something like this
    <html>
    <head>
    </head>
    <body>

    </Form>
    <?php
    $name = $_REQUEST[name];
    if($name == NULL)
    {
    print('<Form action=info.php method=POST><input type="text" name="name"/><input type="submit"/></FORM>');
    }
    else
    {
    print($name);
    }
    ?>
    </body>
    </html>

    however when I tried to run the program
    it gives me

    <TITLE>400 Bad Request</TITLE>
    </HEAD><BODY>
    <H1>Bad Request</H1>
    Your browser sent a request that this server could not understand.<P>
    Request header field is missing colon separator.<P>
    <PRE>
    name%3Dkkkkk</PRE>
    <P>
    </BODY></HTML>
    I only encountered the above problem if i included the POST data content in the request

    fprintf(http->file, "Content-Length: 10\r\n\r");
    fprintf(http->file, "name=kkkkk\r\n");

    do you guys have any idea where did i go wrong ?
    thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Overtaker
    I only encountered the above problem if i included the POST data content in the request
    Code:
    fprintf(http->file, "Content-Length: 10\r\n\r");
    fprintf(http->file, "name=kkkkk\r\n");
    do you guys have any idea where did i go wrong ?
    Merely a guess -- is the extra \r supposed to be there?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by Dave_Sinkula
    Merely a guess -- is the extra \r supposed to be there?
    Nearly, the request headers should be terminated by cr-lf-cr-lf, so the final '\n' is missing.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    sorry I don't get it, what is cr-lf ?
    is it supposed to be like this ?

    Code:
    fprintf(http->file, "Content-Length: 10\r\n");
    fprintf(http->file, "name=kkkkk\r\n\n");

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    No. The \r and \n should come in pairs (\r\n). As was previously noted, you ended up doing 2 \r in a row (you're missing a \n).
    If you understand what you're doing, you're not learning anything.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Of course knowing which machine you're running on would help.
    As would whether said "file" is opened in binary or text mode.

    Then perhaps we could figure out which set of end-of-line translations are being performed (or not).

    @OP.
    Get yourself a copy of www.ethereal.com and compare what a real web browser does with the request compared to what your code produces.
    The answer is in the difference between the two.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    First you must connect to the correct server, then, send a request like this:
    Code:
    fprintf(http->file, "POST /index.php HTTP/1.1\r\n");
    fprintf(http->file, "Host: www.yourhost.com\r\n");
    fprintf(http->file, "Content-Type: applicationx-www-form-urlencoded\r\n");
    fprintf(http->file, "Content-Length: 10\r\n\r\n");
    fprintf(http->file, "name=kkkkk\r\n\r\n");
    You must use relative path from the server root after post, not the host name of that site.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Hi
    thanks for the replies

    I tried
    Code:
    fprintf(http->file, "POST /index.php HTTP/1.1\r\n");
    fprintf(http->file, "Host: www.yourhost.com\r\n");
    fprintf(http->file, "Content-Type: applicationx-www-form-urlencoded\r\n");
    fprintf(http->file, "Content-Length: 10\r\n\r\n");
    fprintf(http->file, "name=kkkkk\r\n\r\n");

    but I got this instead

    Code:
    HTTP/1.1 200 OK
    Date: Thu, 07 Sep 2006 14:59:32 GMT
    Server: Apache/1.3.34 (Unix) mod_perl/1.29
    Transfer-Encoding: chunked
    Content-Type: text/html
    
    95
    <html>
    <head>
    </head>
    <body>
    
    </Form>
    <Form action=info.php method=POST><input type="text" name="name"/><input type="submit"/></FORM></body>
    </html>
    
    0
    This is just my guess, if
    Code:
    fprintf(http->file, "POST /index.php HTTP/1.1\r\n");
    fprintf(http->file, "Host: www.yourhost.com\r\n");
    fprintf(http->file, "Content-Type: applicationx-www-form-urlencoded\r\n");
    fprintf(http->file, "Content-Length: 10\r\n\r\n");
    supposed to be the headers

    then
    Code:
    fprintf(http->file, "name=kkkkk\r\n\r\n");
    supposed to be the body, maybe I need to write it on difference phase instead all of them together ?

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    No-no-no... It receives the whole thing and then sends the whole thing, you can't do it like that.
    You just have to find a way to separate them from each other

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    well i still cant seem to get around it at all
    the server im sending the POST request is Apache
    I read somewhere that the Server will wait if I actually just send up to Content-Length: 10\r\n\r\n
    but when i tried to send just up to that, the Server doesn't seem to wait cos' im getting the Response straight away


    will pasting my entire code here help ?

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This is normal. All browsers separate headers and content and then read the headers and display the content. There is no way to get only the body because header is a critical part of a web page. Better start thinking how you are going to separate header and content.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  2. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. I Need A Example For Http Post
    By rjhome in forum C Programming
    Replies: 5
    Last Post: 02-17-2002, 10:49 AM