Search
 
SCRIPT & CODE EXAMPLE
 

C

send http request in C

#include <stdio.h> /* printf, sprintf */
#include <stdlib.h> /* exit */
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */

void error(const char *msg) { perror(msg); exit(0); }

int main(int argc,char *argv[])
{
    /* first what are we going to send and where are we going to send it? */
    int portno =        80;
    char *host =        "api.somesite.com";
    char *message_fmt = "POST /apikey=%s&command=%s HTTP/1.0

";

    struct hostent *server;
    struct sockaddr_in serv_addr;
    int sockfd, bytes, sent, received, total;
    char message[1024],response[4096];

    if (argc < 3) { puts("Parameters: <apikey> <command>"); exit(0); }

    /* fill in the parameters */
    sprintf(message,message_fmt,argv[1],argv[2]);
    printf("Request:
%s
",message);

    /* create the socket */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) error("ERROR opening socket");

    /* lookup the ip address */
    server = gethostbyname(host);
    if (server == NULL) error("ERROR, no such host");

    /* fill in the structure */
    memset(&serv_addr,0,sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(portno);
    memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);

    /* connect the socket */
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");

    /* send the request */
    total = strlen(message);
    sent = 0;
    do {
        bytes = write(sockfd,message+sent,total-sent);
        if (bytes < 0)
            error("ERROR writing message to socket");
        if (bytes == 0)
            break;
        sent+=bytes;
    } while (sent < total);

    /* receive the response */
    memset(response,0,sizeof(response));
    total = sizeof(response)-1;
    received = 0;
    do {
        bytes = read(sockfd,response+received,total-received);
        if (bytes < 0)
            error("ERROR reading response from socket");
        if (bytes == 0)
            break;
        received+=bytes;
    } while (received < total);

    /*
     * if the number of received bytes is the total size of the
     * array then we have run out of space to store the response
     * and it hasn't all arrived yet - so that's a bad thing
     */
    if (received == total)
        error("ERROR storing complete response from socket");

    /* close the socket */
    close(sockfd);

    /* process response */
    printf("Response:
%s
",response);

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: c how to get an integer from user input 
C :: add border to image android 
C :: %hd c 
C :: binary search in c 
C :: lsusb command not found 
C :: Prime Number Check Program in C 
C :: Successeur récurssive 
C :: operators priority in c 
C :: how to read character from a string in c 
C :: pass the pointer to the function 
C :: const godot gdscript 
C :: reverse list in C 
C :: svg not loading in chrome 
C :: c random array 
C :: xor swap 
C :: read string with space c 
C :: comment c 
C :: toupper function in c 
C :: count distinct characters in a string C 
C :: arduino sketch structure 
C :: loading builder in flutter 
C :: server client tcp in C 
C :: pointers to a function in c 
C :: number pattern in c 
C :: rfid rc522 code 
C :: 2d array in c 
C :: c program to find minimum of 5 numbers using conditional operator in c 
C :: getline function in c 
C :: %g and %e in c 
C :: realloc in c 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =