使用CPP手动构建POST请求,并使用它提交php表单
Constructing a POST request manually with CPP and submitting a php form with it
我构建了下一个请求:
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;
}
相关文章:
- GTK 最大化表单 C++
- win 表单应用程序字符串^ 到 int
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 可以在JSON文件的帮助下在Qt中创建UI表单
- 表单未显示在凭据提供程序中
- 表单显示对话框函数错误,并且不执行下面的语句
- 如何从 winapi 中的编辑表单获取文本?
- 在 cout 中将线条表单文件与自定义文本组合时出现问题
- 从 dll 调用 opencv 垫到 Windows 表单,图像出现故障
- 如何在不使用任何数据库的情况下制作动态注册表单?
- 家庭作业帮助 - 检查表单按钮上的数据
- C++ 在给定像素数据数组的表单上绘制矩形
- 复制初始化表单 '= {}'
- 如何创建第二个 QT .ui 表单
- 如何将变量从一个表单传递到另一个表单
- 使用CPP手动构建POST请求,并使用它提交php表单
- POCO C 简单表单提交样本不起作用
- C++14 cURLcpp提交表单
- 如何执行表单提交
- 编译在在线 HTML 表单上提交的代码并使用 GCC 进行处理的过程