本文介绍了使用HTTP上传文件。获取错误: - HttpSendReuest 12005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I want to upload "D:\er.txt" to webserver using HTTP, when I am running program, i got HttpSendRequest 12005 as an error. i used a PHP script on my webserver that will accept the file and stores it in a pre-made directory named as "upload".. here is my tiny program

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);
}
?>


推荐答案

根据, lplpszAcceptTypes 参数应从

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

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

\0

换句话说,您需要更改

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

这篇关于使用HTTP上传文件。获取错误: - HttpSendReuest 12005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 03:32