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

Constructing a POST request manually with CPP and submitting a php form with it

本文关键字:提交 表单 php CPP 构建 请求 POST 使用      更新时间:2023-10-16

我构建了下一个请求:

char* messege = "POST / HTTP / 1.1rn"
"Host: musicshare.pe.hu rn"
"Content-Length: 18rn"
"Content-Type: text/plainrn"
"Accept: text/plainrnrn"
"command=michaeliko";

这个请求是我通过查看一些捕获wintin Wireshark而建立的,因为我没有找到任何合适的指南。

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

例如,一个常规的铬捕获响应将如下所示:

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

对于我在上面展示的POST REQUEST,我将从网站服务器上获得:

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

php端如下所示:

<?php
if(  isset($_POST['command']))
{
$command = $_POST['command'];
echo 'Hello ' . $command . "n";
}
?>

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

我试图通过多种方式来操纵这个代码,但没有找到答案。我的请求出了什么问题?

我还不能发表评论,所以不能提问,比如:你是如何提交数据的?你想从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: $HeaderValn";
}
}
// Generate the request
$ReqHeader =
"POST $URI HTTP/1.0n".
"Host: $Hostn".
"User-Agent: PostIt 2.0n".
$AuthHeader.
$OtherHeaders.
"Content-Type: application/x-www-form-urlencodedn".
"Content-Length: $ContentLengthnn".
"$ReqBodyn";
// 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 != "0rn") {
$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;
}