Search code examples
csocketshttpauthenticationposix

How to send a login POST/GET request and receive the response in c


i'm learning about POSIX sockets in C language, and i'm curios on how to send a login request (for example Facebook) and actually verify that the login was succesfull. I know how i can retrieve the HTTP request, but i don't know how to past it in a c function, and how to send it with sockets.

I'm not asking for a full code implementation, i'm just asking what are the functions/libraries that could help me, how to use them and the steps i need to follow to accomplish the goal.

I really appreciate any help.


Solution

  • here is a snippet with comments to show you how to make a simple GET Request in C (Take note: I coded this with POSIX in mind)!:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<sys/socket.h>
    #include<arpa/inet.h>
    #include<netinet/in.h>
    
    #define flags 0
    
    
    int main() {
    
            int sock; // Sockets are int data type
            sock = socket(AF_INET, SOCK_STREAM, 0); //AF_INET = Internet Connection //SOCK_STREAM = TCP
    
            struct sockaddr_in server_host; //Struct type is a data type consisting of other data types inside. Similar to a 'class'
    
            struct timeval timeout;
            timeout.tv_sec = 1;  //Set Socket Timeout After 1 Second
            timeout.tv_usec = 0; //Therefore it will stop sending/receiving data
            setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
    
            server_host.sin_family = AF_INET; //Family type of the connection
            server_host.sin_addr.s_addr = inet_addr("216.58.212.110"); //google.com's IP Address
            server_host.sin_port = htons(80); //HTTP Port = 80
    
            connect(sock, (struct sockaddr*)&server_host, sizeof(server_host));
            //This statement means:
            //Connect using the socket                                - connect(sock...
            //Convert 'server_host' to a 'sockaddr' type              - (struct sockaddr*)&server_host...
            //With a memory size of 'server_host' (however big it is) - sizeof(server_host)
    
    
            const char get_request[] = "GET / HTTP/1.0\r\n\r\n"; //This is a GET request. It asks for the site.
    
            send(sock, get_request, strlen(get_request), flags); //Send the get request
            //Send using socket 'sock' - send(sock...
            //Send data 'get_request'    - get_request, ...
            //How long 'get_request' is  - strlen(get_request), ...
    
            char * received_data = (char *)malloc(sizeof(char)*10000); //Gives 'received_data' 10000 From the RAM
            //recv(sock, received_data, 4096, flags);
            //Receive data using socket 'sock'     - recv(sock...
            //Put the data into 'received_data'      - received_data, ...
            //How much of 'received_data' to take up - 4096, ...
    
            do {
                    char byte[1];
                    recv(sock, byte, 1, flags);
                    if(strcmp(byte, "") == 0) {
                            break;
                    } else {
                            strcat(received_data, byte);
                            memset(byte,'\0',sizeof(byte));
                    }
            } while(1==1);
    
            puts(received_data);
    
            close(sock);
            free(received_data); //REMEMBER TO DO THIS!!!!
            return 0;
    }
    

    I hope this helps!