Dota
Dota

Reputation: 53

Following HTTP response header using socket

I have a problem in my code I did send HTTP request to facebook server but the problem i get HTTP/1.1 301 Moved Permanently message. What i want to do is to allow follow the header response link and give me the redirected page HTML, Also I tried to send another HTTP request but I get no response so i thought there is function option that allow me to follow the header link.

So basically what i'm trying to do is this send http post request to facebook server with my user and password log in follow header redirect link give me response.

if it's possible please guide me to the right way to do it. PS: I already read some RFC and it's still not clear. Thanks for your time.

My code

#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 <iostream>
#include <string>
#include <cstring>
#include <cstdlib> 
using namespace std;

int main()
{
    int sockid, status;
    unsigned strSize; 
    ssize_t count; // 

    struct sockaddr_in addr; // struct 
    ssize_t size = sizeof(addr);

    sockid = socket(PF_INET, SOCK_STREAM, 0); // create socket

    if(sockid < 0) //if error 
    {
        cout<<"Cannot create sucket\n";
        close(sockid);
        exit(EXIT_FAILURE);
    }
    // access struct 
    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    if(inet_aton("31.13.90.36",&addr.sin_addr) == 0)
    {
        cout << "Wrong address";
        close(sockid);
        exit(-1);
    }

    status = connect(sockid, (sockaddr*)&addr, sizeof(addr)); // attempt to establish a connection 
    // check
    if(status < 0)
    {
        cout << "failed to establish a connection";
        close(sockid);
        exit(EXIT_FAILURE);
    }

    // sending HTTP request 
     char msg[] = "POST /login.php HTTP/1.1\r\n"
                  "HOST: m.facebook.com\r\n"
                  "Content-type: application/x-www-form-urlencoded\r\n"
                  "Content-Length: 147\r\n"
                  "Connection: Keep-Alive\r\n"
                  "\r\n"
                  "lsd=AVp5UV4F&version=1&ajax=0&width=0&pxr=0&gps=0&dimensions=0&m_ts=1481464104&li=KFlNWFL78UFJkrUnTV_sFFDQ&email=Minglixe&pass=test123&login=Log+In";



    const int bufSize = 4000;    
    char * buf;
    buf = new char [bufSize];
    //unsigned bufSize = strlen(buf);
    strSize = strlen(msg);


    send(sockid, msg, strSize, 0); // send request 

    while((count = recv(sockid, buf, bufSize, 0)) > 0)// receive the request 
    {
        buf[count] = '\0';
        cout << buf << flush;

    }    

   if(count < 0 ) 
    {   
        cout << "error" ;
        exit(EXIT_FAILURE);
    }

   delete[] buf;

    close(sockid);

}

Upvotes: 0

Views: 1517

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

You will have to issue another HTTP request, a GET request, to the redirected URL given in the 301 response.

You will need to close this socket, and create a new socket for the new request. If the redirected URL is for the same domain, you could implement HTTP/1.1 pipelining and save yourself the trouble of tearing down one socket and creating a new one, but that would be overkill.

In addition, it is a near certainty that the 301 response also gave you a handful of cookies, which Facebook's server will expect you to return to it, in the redirected request. Otherwise none of what you're trying to do will have any chance of working.

Unfortunately, there are no shortcuts here, and no instant gratification. In order to do this correctly, you need to understand, and be familiar with, pretty much, the entirety of HTTP. The canonical reference for HTTP consists of RFC 7230 and RFC 7231 (there's also RFC 7232, RFC 7233, RFC 7234, RFC 7235, RFC 7236 and RFC 7237, but I don't believe you need them for your particular purpose). Rather than copy-pasting chunks of it here, I will direct you to the authoritative source, in RFC 7230 and 7231.

HTTP is not really that complicated, but it is not something that can be absorbed in a few hours, like POP3. Be prepared to invest a few days on reviewing that technical specification for HTTP, and studying it. When I implemented my own full-featured HTTP 1.1 client, it took me (in my spare time), a few months to work my way through the RFC 2616 predecessor, and hack up a cookie-supporting HTTP 1.1 client.

Furthermore, in order for your HTTP client to handle cookies, the canonical reference for how to handle cookies is RFC 6265. You will need to learn that too.

P.S. Hopefully the redirected URL is not an https URL, or Facebook permits non-encrypted connections to fetch content after logging in. Otherwise, I'm afraid, you will need to pull in your platform's SSL libraries, and also code a TLS/SSL session on top of the whole thing.

Upvotes: 4

Related Questions