使用CPP手动构造POST请求并使用它提交php表单

时间:2017-02-09 17:04:23

标签: php c++ http http-post

我构建了下一个请求:

DocumentManager

我通过查看一些捕获wintin Wireshark来构建此请求,因为我没有找到任何合适的指南。

我得到一个好的消息,但没有" echo" (打印)命令我在php端做了。

例如,常规的Chrome捕获响应将如下所示:

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";

对于我在上面展示的POST REQUEST,我会从网站服务器上得到这个:

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

php端看起来像这样:

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

我尝试通过多种方式操作此代码,但没有找到答案。 我的要求有什么问题?

1 个答案:

答案 0 :(得分:1)

我还没有发表评论,因此无法提出问题,例如:您是如何提交数据的?您是否尝试从PHP程序执行此操作?以下是我多年前写的一个函数,我不知道它是不是你要找的东西;如果没有,您可以尝试cURL库。

 /*
  * 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;
  }