在 C++ 中使用 libcurl应用程序从 .INI 文件中的值发送 POST 数据
因此,我有一个 C++ 应用程序,它从 settings.INI 文件中的键获取值,使用 libcurl 访问 PHP 页面,发布该数据,并且它应该返回一些不同的数据。
除了从 INI 文件中获取数据之外,它确实有效。如果我明确输入 libcurl 的 POSTFIELDS 选项(ei: "Serial=454534" ,而不是存储从我的文件检索的数据的变量)。
这是一些代码..
PHP:
<?
include("DBHeader.inc.php");
$Serial= $_POST['Serial'];
$sql = "SELECT * FROM `LockCodes` WHERE `sLockCode`=\"$Serial\"";
$RS=mysql_query($sql, $SQLConn);
$num_rows=mysql_num_rows($RS);
if ($num_rows>0)
{
while ($row = mysql_fetch_array($RS))
{
echo $row['iLockCodeID'];
}
}
else
{
echo "...";
}
?>
C++ 代码片段:
TCHAR szKeyValue[36];
GetPrivateProfileString(_T("Test"), _T("LockCode"), _T(""), szKeyValue, 36, _T("C:\\Test\\Settings.INI"));
CString sLockCode = szKeyValue;
CURL *curl;
CURLcode res;
CString Serial = _T("Serial=") + sLockCode;
string LCID;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://regserver2.nyksys.com/GetLCID.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Serial);
res = curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (data == _T("..."))
{
data = "0";
AfxMessageBox("Invalid Serial Number");
exit(0);
}
我的 Settings.INI 采用标准格式。
[Test]
LockCode=1D4553C7E7228E462DBAAE267977B7CDED8A
发生的情况是,每当我使用变量“Serial”而不是键入它时,PHP 页面都会返回“...”而不是所需的结果。
我觉得我错过了一些明显的东西。任何帮助将非常感激。
So I have a C++ application that takes a value from a key in a settings.INI file, uses libcurl to reach out to a PHP page, post that data, and it should return some different data.
And it actually works, aside from grabbing the data from the INI file. If I explicitely type in for the POSTFIELDS option for libcurl (e.i.: "Serial=454534" , instead of the variable storing the data that it retrived from my file).
Here's some code..
PHP:
<?
include("DBHeader.inc.php");
$Serial= $_POST['Serial'];
$sql = "SELECT * FROM `LockCodes` WHERE `sLockCode`=\"$Serial\"";
$RS=mysql_query($sql, $SQLConn);
$num_rows=mysql_num_rows($RS);
if ($num_rows>0)
{
while ($row = mysql_fetch_array($RS))
{
echo $row['iLockCodeID'];
}
}
else
{
echo "...";
}
?>
Snippet of C++ code:
TCHAR szKeyValue[36];
GetPrivateProfileString(_T("Test"), _T("LockCode"), _T(""), szKeyValue, 36, _T("C:\\Test\\Settings.INI"));
CString sLockCode = szKeyValue;
CURL *curl;
CURLcode res;
CString Serial = _T("Serial=") + sLockCode;
string LCID;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://regserver2.nyksys.com/GetLCID.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Serial);
res = curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (data == _T("..."))
{
data = "0";
AfxMessageBox("Invalid Serial Number");
exit(0);
}
My Settings.INI is in the standard format..
[Test]
LockCode=1D4553C7E7228E462DBAAE267977B7CDED8A
What happens is whenever I use the variable "Serial" instead of typing it in, the PHP page returns "..." instead of the desired result.
I feel like I'm missing something obvious. Any help would be TREMENDOUSLY appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测您已经定义了
_UNICODE
,这意味着TCHAR
是wchar_t
,CString
是CStringT< ;wchar_t>
,以及代码:当函数需要窄字符字符串时,实际上将宽字符字符串传递给
curl_easy_setopt()
。如果您的计算机是 Little Endian,则curl_easy_setopt()
会将参数解释为字符串"S\x00e\x00r\x00i\x00...
(在 Big Endian 计算机上) ,它是"\x00S\x00e\x00r\x00i...
) 并且因为curl_easy_setopt()
会当CURLOPT_POSTFIELDSIZE
未设置时使用strlen()
,在小端机器上整个POST请求体是S
。例如,请参阅 http://codepad.org/JE2MYZfU您需要做的是使用窄字符字符串:
此外,您的 PHP 脚本看起来很容易受到 SQL 注入的攻击。
编辑:还有一件事。您是否将
CURLOPT_POST
设置为 1?I am guessing that you have
_UNICODE
defined, which means thatTCHAR
iswchar_t
,CString
isCStringT<wchar_t>
, and the code:actually passes a wide char string to
curl_easy_setopt()
when the function is expecting a narrow char string. If your machine is little Endian, thencurl_easy_setopt()
interprets the parameter as the string"S\x00e\x00r\x00i\x00...
(on a Big Endian machine, it's"\x00S\x00e\x00r\x00i...
) and becausecurl_easy_setopt()
will usestrlen()
whenCURLOPT_POSTFIELDSIZE
is not set, the entire POST request body on a little Endian machine isS
. See, for example, http://codepad.org/JE2MYZfUWhat you need to do is use narrow char strings:
Also, your PHP script looks vulnerable to SQL injection.
EDIT: One more thing. Are you setting
CURLOPT_POST
to 1?