我将使用wininet上传text.txt
文件
这是一个代码。
#include "stdafx.h"
#include "iostream"
#include "windows.h"
#include "wininet.h"
#include "tchar.h"
#include "iostream"
#include "string"
#include "sstream"
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Wininet.lib")
int main(){
TCHAR hdrs[] = _T("Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858");
TCHAR frmdata[] = _T("-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\Users\Ahmer\Desktop\asd\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n");
LPCTSTR accept[] = {_T("*/*"), NULL};
HINTERNET hSession = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, _T("http://localhost/cpp/"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("uploadfile.php"), NULL, NULL, accept, 0, 1);
BOOL sent = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, _tcslen(frmdata));
if(hSession==NULL)
{
cout<<"Error: InternetOpen";
}
if(hConnect==NULL)
{
cout<<"Error: InternetConnect";
}
if(hRequest==NULL)
{
cout<<"Error: HttpOpenRequest";
}
if(!sent)
{
cout<<"Error: HttpSendRequest";
}
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 0;
}
这是一个php文件。
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/FileFolder");
错误Error: HttpOpenRequest
。这段代码有什么问题。
我从这里读了一个问题,但在这个问题中,用户没有粘贴代码,而是给出现在关闭的网站链接,所以现在没有用了。
Dev C++ Wininet Upload file using HTTP
但是我从那里找到了php代码。