1
votes

I want to send some binary data to a php http server via HTTP POST method from a pic32 embedded board . my server will reply with data which he will receive. I get 0x20 instead of 0x2b ('+') from server.

my function is

void send_post_query(void)
{
        char *hname = "www.example.com";
        char *page = "/test/testAES.php";
        char *poststr = "status=";
        char * data_string ="This is to be encrypted before sending";
        unsigned char encoded_data[512];
        unsigned int encoded_data_length;
        char status_string[512];
        unsigned char data_string[512];
        unsigned char header_lenght=0;

        //Encode string 
        encoded_data_length= EncryptSendingPacket(data_string,strlen(data_string),encoded_data);

        //create query
        snprintf(status_string, 512,
         "POST %s HTTP/1.1\r\n"
         "Host: %s\r\n"
         "Content-type: application/x-www-form-urlencoded\r\n"
         "Content-length: %d\r\n\r\n"
                 "%s", page, hname, encoded_data_length+strlen(poststr),poststr);
        header_lenght=strlen(status_string);
        //Append data through string
        memcpy(status_string+header_lenght,encoded_data,encoded_data_length);
        //Send data through an open socket
        send(ulSocket, status_string, header_lenght+encoded_data_length,0);

}

I found some post regarding the usage of 'Content-Type': 'application/octet-stream'. but on changing this ,not even any byte received from server. (simply i don't know how to use 'Content-Type': 'application/octet-stream' from c ).Please help me to fill this http post header for sending binary data.

Any help will be thankful.

1

1 Answers

2
votes

One problem I see is that you're copying your binary data (unsigned char) into a "string" (char) variable.

The easiest way I see is to issue two send() calls:

  • 1 for the header data
  • 1 for the binary data (body)

Also, you'll need to set your content type to "application/octet-stream". A content type of "x-www-form-urlencoded" is completely wrong for binary data.