1
votes

I've been trying to send HTTP POST Request to facebook without success i get this response from the server :

HTTP/1.1 400 Bad Request Content-Type: text/html; charset=utf-8 Date: Sat, 10 Dec 2016 21:28:17 GMT Connection: close Content-Length: 2959

Facebook | Error

Sorry, something went wrong We're working on it and we'll get it fixed as soon as we can

My code

#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <fstream>
using namespace std;

int main()
{
    int s, error;
    struct sockaddr_in addr;


        s = socket(AF_INET, SOCK_STREAM, 0);
    if(s <0)
    {
        cout<<"Error 01: creating socket failed!\n";
        close(s);
        return 1;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    inet_aton("31.13.90.36",&addr.sin_addr);

    error = connect(s,(sockaddr*)&addr,sizeof(addr));
    if(error!=0)
    {
        cout<<"Error 02: conecting to server failed!\n";
        close(s);
        return 1;
    }

     const int msgSize = 1042;
     char msg[] = "POST /login.php?login_attempt=1 \r\n"
                     "HTTP/1.1\r\n"
                     "HOST: facebook.com\r\n\r\n"
                     "Content-type: application/x-www-form-urlencoded\r\n"
                     "Content-Length: 41\r\n"
                     "[email protected]&pass=test123&submit=1\r\n" ;







     char answ[1042];

    //cin.getline(&msg[0],256);

    send(s,msg,strlen(msg),0);


    ssize_t len;
    while((len = recv(s, msg, strlen(msg), 0)) > 0)
    {
        cout.write(msg, len);

        std::cout << std::flush;
    }
    if(len < 0)
    {
        cout << "error";
    }
    close(s);  

}

What I did wrong?

1

1 Answers

4
votes

There are several errors in your message. This is what you send according to your code:

1  POST /login.php?login_attempt=1 \r\n
2  HTTP/1.1\r\n 
3  HOST: facebook.com\r\n\r\n
4  Content-type: application/x-www-form-urlencoded\r\n
5  Content-Length: 41\r\n
6  [email protected]&pass=test123&submit=1\r\n

Instead it should be like this:

1  POST /login.php?login_attempt=1 HTTP/1.1\r\n
2  HOST: facebook.com\r\n
3  Content-type: application/x-www-form-urlencoded\r\n
4  Content-Length: 41\r\n
5  \r\n
6  [email protected]&pass=test123&submit=1

In detail:

  • Line 1 and 2 should be a single line, i.e. "method path HTTP-version"
  • Line 3 should not contain multiple \r\n
  • instead the empty line \r\n should be after all HTTP headers (new line 5)
  • the body in line 6 should contain only the data covered by the content-length. The 41 bytes you set there don't include the \r\n you send

Apart from that you don't properly parse the response and instead expect the server to close the connection once the response is done. Since you are using HTTP/1.1 you implicitly use persistent connections (HTTP keep-alive) so the server might actually wait for more requests within the same TCP connection and not close the connection immediately.

I really recommend that you study the standard of HTTP instead of guessing how the protocol might work.