1

I want to create an HTTP post request in C. I have success to send Get Method but, when I try with Post, program is just stopped. Here the code :

#include<stdio.h>
#include<winsock2.h>
#include <string.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;
    char *message , server_reply[2000];
    int recv_size;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");


    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 80 );

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected");

    // Work well with Get Method
    // strcat(message, "GET /voleur/receiver.php?arg1=Hello HTTP/1.1\n");
    strcat(message, "POST /voleur/receiver.php HTTP/1.1\n");
    strcat(message, "Host: 127.0.0.1\n");
    strcat(message, "Connection: close\n");
    strcat(message, "Content-Type:application/octet-stream\n");
    strcat(message, "Content-Encoding:binary\n");
    strcat(message, "Content-Length:16\n");
    strcat(message, "Accept-Charset: utf-8\n\n");



    if( send(s , message , strlen(message) , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }

    // My argument
    char *data = "arg1=Hello";
    send(s , data , strlen(data), 0);
    puts("Data Send\n");

    //Receive a reply from the server
    if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
    {
        puts("recv failed");
    }

    puts("Reply received\n");

    //Add a NULL terminating character to make it a proper string before printing
    server_reply[recv_size] = '\0';
    puts(server_reply);

    return 0;
}  
developer
  • 4,744
  • 7
  • 40
  • 55
Wibi Cahyo
  • 39
  • 1
  • 7

1 Answers1

2

First: You should reformat your HTTP header:

strcat(message, "Content-Type:application/octet-stream\n");
strcat(message, "Content-Encoding:binary\n");
strcat(message, "Content-Length:16\n");

should be

strcat(message, "Content-Type: application/octet-stream\n");
strcat(message, "Content-Encoding: binary\n");
strcat(message, "Content-Length: 16\n");

Second: As far as I can see, your problem lies within the following lines:

strcat(message, "Content-Length: 16\n");
[...]
char *data = "arg1=Hello";
send(s , data , strlen(data), 0);

because you declare there will be 16 bytes of data - but you only send 10 bytes, which will cause the server to wait for the remaining 6 bytes.

Daniel Heinrich
  • 790
  • 4
  • 12
  • There is no difference between the first block of `strcat`s and the second block of `strcat`s. Please correct. – Jabberwocky Nov 18 '16 at 08:38
  • 2
    Yes there is - I added spaces between the colons an the header field values. – Daniel Heinrich Nov 18 '16 at 08:40
  • 1
    Oh, yes now I see it. You should point that out in your answer. – Jabberwocky Nov 18 '16 at 08:44
  • Thanks Daniel, I change code to this : strcat(message, "POST /voleur/receiver.php HTTP/1.1\n"); strcat(message, "Host: 127.0.0.1\n"); strcat(message, "Connection: Keep Alive\n"); strcat(message, "Content-Type: application/x-www-form-urlencoded\n"); strcat(message, "Content-Encoding: binary\n"); strcat(message, "Content-Length: 10\n"); strcat(message, "Accept-Charset: utf-8\n\n"); strcat(message, "arg1=Hello"); the program is not stopped again, but post data is empty in my php, can you help me find my mistake ... – Wibi Cahyo Nov 18 '16 at 10:00