gpt4 book ai didi

php - 使用 HTTP 上传文件。收到错误 :- HttpSendReuest 12005

转载 作者:行者123 更新时间:2023-11-28 07:28:13 25 4
gpt4 key购买 nike

我想使用 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);
}
?>

最佳答案

根据 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);

关于php - 使用 HTTP 上传文件。收到错误 :- HttpSendReuest 12005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18307919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com