【问题标题】:Upload file using HTTP. getting error :- HttpSendReuest 12005使用 HTTP 上传文件。出现错误:- HttpSendReuest 12005
【发布时间】:2013-08-20 21:39:55
【问题描述】:

我想使用 HTTP 将“D:\er.txt”上传到网络服务器,当我运行程序时,我收到 HttpSendRequest 12005 作为错误。我在我的网络服务器上使用了一个 PHP 脚本,它将接受文件并将其存储在名为“上传”的预制目录中。这是我的小程序

int main()
{
    static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"D:\\er.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents  here\r\n-----------------------------7d82751e2bc0858--\r\n";
    static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";

    HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if(!hSession)
    {
        cout<<"Error: InternetOpen";
    }


    HINTERNET hConnect = InternetConnect(hSession, _T("http://jobnews.netii.net/upload.php"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    if(!hConnect)
    {
        cout<<"Error: InternetConnect";
    }

    HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
    if(hRequest==NULL)
    {
        cout<<"Error: HttpOpenRequest";
    }

    BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
    if(!sent)
    {
        cout<<"Error: HttpSendRequest "<<GetLastError();
    }


    //close any valid internet-handles
    InternetCloseHandle(hSession);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hRequest);
    getchar();
    return 0;
}

我的 PHP 脚本是

<?php
$uploaddir = 'upload/'; // Relative Upload Location of data file

if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
$uploadfile = $uploaddir . basename($_FILES['uploadedfile']['name']);
echo "File ". $_FILES['uploadedfile']['name'] ." uploaded successfully. ";
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully moved. ";
}

else
print_r($_FILES);
}

else {
echo "Upload Failed!!!";
print_r($_FILES);
}
?>

【问题讨论】:

  • 这是某种任务吗? stackoverflow.com/questions/1985345/…
  • 不不.. 一点也不。我是开发领域的初学者,我试图通过一些工作示例来理解这些概念。这只是为了学习目的
  • 您引用的错误消息是“无效的 URL”。如果其他人关于滥用字符串引用的说法是正确的,那将是有道理的。

标签: php c++ file http upload


【解决方案1】:

根据HttpOpenRequest 的文档,lplpszAcceptTypes 参数应该从

(const char**)"*/*\0"

{_T("*/*"), NULL}

您还可以从字符串末尾删除\0。您无需手动将 nul 终止符插入到字符串文字中。

换句话说,你需要改变

HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",
                                     _T("upload.php"), NULL, NULL,
                                     (const char**)"*/*\0", 0, 1);

LPCTSTR rgpszAcceptTypes[] = {_T("*/*"), NULL};
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",
                                     _T("upload.php"), NULL, NULL,
                                     rgpszAcceptTypes, 0, 1);

【讨论】:

  • @user2682006 他说像他链接的文档一样传递LPCTSTR* 是必需的。您的演员表不仅不正确,而且无效。 LPCTSTR arAccentTypes[] = {_T("*/*"), NULL};。传递 that (按值,不需要间接,强制转换等)。
  • @user2682006 我已经更新了我的答案以更明确地说明问题。
  • 先生,我正在使用 DEV C++,在将您的行放入其中后,它显示 `PCTSTR' undeclared (first use this function) 错误
  • 我可能从 msdn 复制的太随意了,请尝试改用 LPCTSTR。有关这两种类型的一些详细信息,请参阅this previous post
  • 替换您建议的一些行后,我的程序编译成功,但在调试时仍然显示相同的错误“HttpSendRequest 12005”作为错误:(
猜你喜欢
  • 1970-01-01
  • 2019-09-25
  • 2018-10-15
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
相关资源
最近更新 更多