0

I built the next request:

char* messege = "POST / HTTP / 1.1\r\n"
    "Host: musicshare.pe.hu \r\n"
    "Content-Length: 18\r\n"
    "Content-Type: text/plain\r\n"
    "Accept: text/plain\r\n\r\n"
    "command=michaeliko";

This request I built by looking at some capturing wintin Wireshark because I didnt find any appropriate guide to it.

I get an OK messege OK on it, but without the "echo" (print) command I did on the php side.

So for example, a regular chrome capture respond will look like this:

   Hello michaeliko\n
    \n
    \n
    \n
    <form method="post">\n
    \n
      Subject: <input type="text" name="command"> <br>\n
      \n
    </form>\t

And for the POST REQUEST I showed abouve, I will get this from the website server:

    \n
    \n
    \n
    <form method="post">\n
    \n
      Subject: <input type="text" name="command"> <br>\n
      \n
    </form>\t

The php side looks like this:

<?php

if(  isset($_POST['command']))
{
  $command = $_POST['command'];
  echo 'Hello ' . $command . "\n";
}

?>



<form method="post">

  Subject: <input type="text" name="command"> <br>

</form> 

I tried to manipulate this code by many ways but find no answer. What is wrong with my request?

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
wrg wfg
  • 77
  • 6

1 Answers1

1

I'm not able to comment yet, so unable to ask questions, such as: How are you submitting the data? Are you trying to do this from a PHP program? Below is a function I wrote years ago, I don't know if it is what you are looking for; if not, you might try the cURL library.

 /*
  * POST data to a URL with optional auth and custom headers
  *   $URL        = URL to POST to
  *   $DataStream = Associative array of data to POST
  *   $UP         = Optional username:password
  *   $Headers    = Optional associative array of custom headers
  */
  function hl_PostIt($URL, $DataStream, $UP='', $Headers = '') {
    // Strip http:// from the URL if present
    $URL = preg_replace('=^http://=', '', $URL);

    // Separate into Host and URI
    $Host = substr($URL, 0, strpos($URL, '/'));
    $URI = strstr($URL, '/');

    // Form up the request body
    $ReqBody = '';
    while (list($key, $val) = each($DataStream)) {
      if ($ReqBody) $ReqBody.= '&';
      $ReqBody.= $key.'='.urlencode($val);
    }
    $ContentLength = strlen($ReqBody);

    // Form auth header
    if ($UP) $AuthHeader = 'Authorization: Basic '.base64_encode($UP)."\n";

    // Form other headers
    if (is_array($Headers)) {
      while (list($HeaderName, $HeaderVal) = each($Headers)) {
        $OtherHeaders.= "$HeaderName: $HeaderVal\n";
      }
    }

    // Generate the request
    $ReqHeader =
      "POST $URI HTTP/1.0\n".
      "Host: $Host\n".
      "User-Agent: PostIt 2.0\n".
      $AuthHeader.
      $OtherHeaders.
      "Content-Type: application/x-www-form-urlencoded\n".
      "Content-Length: $ContentLength\n\n".
      "$ReqBody\n";

    // Open the connection to the host
    $socket = fsockopen($Host, 80, $errno, $errstr);
    if (!$socket) {
      $Result["errno"] = $errno;
      $Result["errstr"] = $errstr;
      return $Result;
    }

    // Send the request
    fputs($socket, $ReqHeader);

    // Receive the response
    while (!feof($socket) && $line != "0\r\n") {
      $line = fgets($socket, 1024);
      $Result[] = $line;
    }

    $Return['Response'] = implode('', $Result);

    list(,$StatusLine) = each($Result);
    unset($Result[0]);
    preg_match('=HTTP/... ([0-9]{3}) (.*)=', $StatusLine, $Matches);

    $Return['Status']   = trim($Matches[0]);
    $Return['StatCode'] = $Matches[1];
    $Return['StatMesg'] = trim($Matches[2]);

    do {
      list($IX, $line) = each($Result);
      $line = trim($line);

      unset($Result[$IX]);

      if (strlen($line)) {
        list($Header, $Value) = explode(': ', $line, 2);

        if (isset($Return[$Header])) {
          if (!is_array($Return[$Header])) {
            $temp = $Return[$Header];
            $Return[$Header] = [$temp];
          }

          $Return[$Header][] = $Value;
        }

        else $Return[$Header] = $Value;
      }
    }
    while (strlen($line));

    $Return['Body'] = implode('', $Result);

    return $Return;
  }
alanlittle
  • 460
  • 2
  • 12
  • No. you send here the request through php, I do it through C++. I dont need to submit with a botton because instead of confirm the form with a bottun, I do it by C++ code. – wrg wfg Feb 09 '17 at 17:33
  • OK, I didn't see that tag when I posted. In that case, you might try [libcurl](https://curl.haxx.se/libcurl/) or [boost](https://boostgsoc14.github.io/boost.http/) – alanlittle Feb 09 '17 at 17:54
  • Yhea yhea man... thats for the weak ;) I want to learn how the things goes and just then I will (maybe...) go the to higher level.. – wrg wfg Feb 09 '17 at 17:57
  • OK, I can dig it :) I'm learning C++ myself, writing a CGI program, and I'm trying to do it with as few libraries as possible. I'm afraid that HTTP POST is beyond my expertise at the moment! (Now if I can just figure out why my linked list isn't working..... ) – alanlittle Feb 09 '17 at 18:22
  • Have you seen [this thread](http://stackoverflow.com/questions/22077802/simple-c-example-of-doing-an-http-post-and-consuming-the-response)? – alanlittle Feb 09 '17 at 18:25
  • Yes of course! But I didnt find the answer there (although it was just infront of my face...). I were tired of this subject, so I copied and paste the all header I captured in Wireshark when sending a POST REQUEST, and I deleted one row after another, untill I found the problem. I searched on it now on google and I understand now the thing I missed. A very important detail. you need to read here http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data good luck my friend :) – wrg wfg Feb 09 '17 at 22:00