使用cpp在web上传数据

时间:2015-03-23 18:33:30

标签: php c++ upload

我正在使用此代码在网上上传数据,但此代码中出现了一些错误 这是一个代码。

static TCHAR hdrs[] =
      _T("Content-Type: application/x-www-form-urlencoded");
static TCHAR frmdata[] =
      _T("name=John+Doe&userid=hithere&other=P%26Q");
static LPSTR accept[2]={"*/*", NULL};

// for clarity, error-checking has been removed
HINTERNET hSession = InternetOpen("MyAgent",
      INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, _T("http://localhost"),
      INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
      _T("/upload.php"), NULL, NULL, accept, 0, 1);
HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
// close any valid internet-handles

这是一个PHP代码。

<?php

 $name = $_POST['name'];
 $userid = $_POST['userid'];
 $other = $_POST['other'];

  $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
  $txt1 = $name."\n";
  fwrite($myfile, $txt1);
  $txt2 = $userid."\n";
  fwrite($myfile, $txt2);
  $txt3 = $other."\n";
  fwrite($myfile, $txt3);
  fclose($myfile);

 ?>

当我编译代码时,它会给出错误,例如从char **到const char **的无效转换。这是一个php文件upload.php它运行良好,我将其与我的cpp文件连接,以便在网上上传数据 任何人都可以帮助我 谢谢。

1 个答案:

答案 0 :(得分:0)

确保将项目配置为使用Unicode。适当的UNICODE兼容代码如下所示:

TCHAR hdrs[] = _T("Content-Type: application/x-www-form-urlencoded");
TCHAR frmdata[] = _T("name=John+Doe&userid=hithere&other=P%26Q");
LPCTSTR accept[] = {_T("*/*"), NULL};

HINTERNET hSession = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, _T("http://localhost"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("/upload.php"), NULL, NULL, accept, 0, 1);
HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, _tcslen(frmdata));