gpt4 book ai didi

php - HttpSendRequest 12005 错误

转载 作者:行者123 更新时间:2023-11-30 04:15:21 25 4
gpt4 key购买 nike

我有一个小程序可以将文件上传到服务器。出于测试目的,我使用了我自己的由 WebAmp 软件创建的本地服务器。我的程序将使用 PHP 脚本上传文件,该脚本将从程序中接受文件并将它们存储到指定位置的服务器上。现在我对此感到困惑,我必须将这个 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("MyBrowser",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(!hSession)
{
cout<<"Error: InternetOpen";
}


HINTERNET hConnect = InternetConnect(hSession, _T("http://localhost"),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);
LPCTSTR rgpszAcceptTypes[] = {_T("*/*"), NULL};
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",
_T("upload.php"), NULL, NULL,
rgpszAcceptTypes, 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 = './'; // 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);
}
?>

最佳答案

lpszServerName 参数应该是主机名,而不是 URL。

http://localhost 更改为 localhost

将此代码放在 HTTPSendRequest 下方以打印响应 header 和内容

//Print the first 2k of the response headers and content
char buffer[2048] = {};
DWORD bufferSize = sizeof(buffer);
BOOL success = HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &bufferSize, NULL);
if(!success)
{
std::cout<<"Error: HttpQueryInfo "<< GetLastError();
return 0;
}
std::cout << buffer << std::endl;

ZeroMemory(buffer, sizeof(buffer));
success = InternetReadFile(hRequest, buffer, sizeof(buffer), &bufferSize);
if(!success)
{
std::cout << "Error: InternetReadFile " << GetLastError();
return 0;
}
std::cout << buffer << std::endl;

关于php - HttpSendRequest 12005 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18373115/

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