在 C++ 中使用 libcurl应用程序从 .INI 文件中的值发送 POST 数据

发布于 2024-12-01 03:45:52 字数 1611 浏览 2 评论 0原文

因此,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2024-12-08 03:45:52

我猜测您已经定义了 _UNICODE,这意味着 TCHARwchar_tCStringCStringT< ;wchar_t>,以及代码:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Serial);

当函数需要窄字符字符串时,实际上将宽字符字符串传递给 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

您需要做的是使用窄字符字符串:

#define ARRAY_LEN(arr_id) ((sizeof (arr_id))/(sizeof ((arr_id)[0])))

char szKeyValue[36];
GetPrivateProfileStringA("Test", "LockCode", "", szKeyValue, ARRAY_LEN(szKeyValue), "C:\\Test\\Settings.INI");

CURL *curl;
CURLcode res;
CStringT<char> Body = CStringT<char>("Serial=") + szKeyValue;
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, Body);
    res = curl_easy_perform(curl);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    res = curl_easy_perform(curl);
//...

此外,您的 PHP 脚本看起来很容易受到 SQL 注入的攻击。

编辑:还有一件事。您是否将 CURLOPT_POST 设置为 1?

    curl_easy_setopt(curl, CURLOPT_POST, 1);

I am guessing that you have _UNICODE defined, which means that TCHAR is wchar_t, CString is CStringT<wchar_t>, and the code:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Serial);

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, then curl_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 because curl_easy_setopt() will use strlen() when CURLOPT_POSTFIELDSIZE is not set, the entire POST request body on a little Endian machine is S. See, for example, http://codepad.org/JE2MYZfU

What you need to do is use narrow char strings:

#define ARRAY_LEN(arr_id) ((sizeof (arr_id))/(sizeof ((arr_id)[0])))

char szKeyValue[36];
GetPrivateProfileStringA("Test", "LockCode", "", szKeyValue, ARRAY_LEN(szKeyValue), "C:\\Test\\Settings.INI");

CURL *curl;
CURLcode res;
CStringT<char> Body = CStringT<char>("Serial=") + szKeyValue;
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, Body);
    res = curl_easy_perform(curl);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    res = curl_easy_perform(curl);
//...

Also, your PHP script looks vulnerable to SQL injection.

EDIT: One more thing. Are you setting CURLOPT_POST to 1?

    curl_easy_setopt(curl, CURLOPT_POST, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文