【发布时间】:2020-03-07 17:02:35
【问题描述】:
我正在尝试使用 wininet 库将文件上传到 C 中的 anonfile。
根据their API,我们需要发送一个指定文件的POST HTTP请求到以下URL:
https://api.anonfile.com/upload
另外,我上传了一个带有 CURL 的文件以查看生成的标题:https://pastebin.com/VZfhaxEy
因此,我的函数看起来像这个 (based on this post)。请注意,它仍然不处理服务器响应,只是发送:
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#pragma comment(lib,"Wininet.lib") //Library
#define ERROR_OPEN_FILE 10
#define ERROR_MEMORY 11
#define ERROR_SIZE 12
#define ERROR_INTERNET_OPEN 13
#define ERROR_INTERNET_CONN 14
#define ERROR_INTERNET_REQ 15
#define ERROR_INTERNET_SEND 16
int main(int argc, char* argv[])
{
// Local variables
char* filename = "C:\\test.zip"; //Filename to be loaded
char* type = "binary";
char boundary[] = "pippo"; //Header boundary
char nameForm[] = "upload"; //Input form name
char iaddr[] = "api.anonfile.com"; //IP address
char url[] = "upload"; //URL
char hdrs[1024]; //Headers
char* buffer; //Buffer containing file + headers
char* content; //Buffer containing file
FILE* pFile; //File pointer
long lSize; //File size
HRESULT result;
// Open file
fopen_s(&pFile, filename, "rb");
if (pFile == NULL) return ERROR_OPEN_FILE;
// obtain file size:
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
content = (char*)malloc(sizeof(char) * lSize);
if (content == NULL) return ERROR_MEMORY;
// copy the file into the buffer:
result = fread(content, 1, lSize, pFile);
if (result != lSize) return ERROR_SIZE;
// terminate
fclose(pFile);
//allocate memory to contain the whole file + HEADER
buffer = (char*)malloc(sizeof(char) * lSize + 2048);
//print header
sprintf_s(hdrs,
1024,
"POST /upload HTTP/2\r\n"
"Host: api.anonfile.com\r\n"
"User-Agent: curl/7.60.0"
"cache-control: no-cache\r\n"
"content-type: multipart/form-data\r\n"
"name=\"%s\"; filename=\"%s\"\r\n"
"Content-Length: %i\r\n"
"Accept: */*\r\n"
"\n", nameForm, filename, lSize);
//Open internet connection
HINTERNET hSession = InternetOpen("WinSock", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession == NULL) return ERROR_INTERNET_OPEN;
HINTERNET hConnect = InternetConnect(hSession, iaddr, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (hConnect == NULL) {
printf("%d", GetLastError());
return ERROR_INTERNET_CONN;
}
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST", url, NULL, NULL, (const char**)"*/*\0", 0, 1);
if (hRequest == NULL) {
printf("%d", GetLastError());
return ERROR_INTERNET_REQ;
}
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
if (!sent) {
printf("%d", GetLastError());
return ERROR_INTERNET_SEND;
}
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 0;
}
但不知何故,它无法执行连接:
123
(process 7652) exited with code 14. (ERROR_INTERNET_CONN).
我错过了什么?
这里是the only anonfile file uploading example I've found,但它使用 curl 并且是 C++ 语言。
【问题讨论】:
-
InternetConnect: "如果连接成功,则返回会话的有效句柄,否则返回 NULL。要检索扩展的错误信息,请调用 GetLastError。应用程序还可以使用 InternetGetLastResponseInfo 来确定拒绝访问服务的原因。"
-
buffer不包含任何内容。你想发送content和lSize(虽然这不能解决你的主要问题)