【问题标题】:C++ cUrl Send mulipart/form-data file to webserverC++ cUrl 将 multipart/form-data 文件发送到 Web 服务器
【发布时间】:2016-11-14 05:16:25
【问题描述】:

正如标题所说,我想在 C++ 程序中将带有 cUrl 库的文件发送到网络服务器。

我从服务器获得了 url 以及它想要 HTTP POST 方法和 mutipart/form-data 的信息。它还需要一个包含数据的参数文件。

我还有一个 cUrl 调用,它可以与 curl 控制台一起使用:

curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "file=@test.txt" "url"

如果我运行下面的代码,服务器会给出没有上传或没有多部分表单的答案。

现在是我的代码:

//## File stuff
struct stat file_info;
FILE *fd;

fopen_s(&fd,"file.txt", "rb"); /* open file to upload */
if (!fd) {

    return 1; /* can't continue */
}

/* to get the file size */
if (fstat(_fileno(fd), &file_info) != 0) {

    return 1; /* can't continue */
}
//##

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "url");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "content-type: multipart / form-data");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(hnd, CURLOPT_READDATA, fd);

curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE,(curl_off_t)file_info.st_size);

CURLcode ret = curl_easy_perform(hnd);

编辑

来自缓冲区的文件的新代码:

std::string contents;
std::ifstream in("empty.txt", std::ios::in | std::ios::binary);

if (in)
{
    in.seekg(0, std::ios::end);
    contents.resize(in.tellg());
    in.seekg(0, std::ios::beg);
    in.read(&contents[0], contents.size());
    in.close();
}

CURL *hnd = curl_easy_init();

uint32_t size = contents.size();

//struct curl_slist *headers = NULL;
//headers = curl_slist_append(headers, "cache-control: no-cache");
//headers = curl_slist_append(headers, "content-type: multipart/form-data");
//curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_URL, "url");
curl_easy_setopt(hnd, CURLOPT_POST, 1);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, contents.data());
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE, size);

CURLcode ret = curl_easy_perform(hnd);

现在服务器响应没有文件数据。我还要说它是多部分/表单数据吗?但是,如果我取消注释标头设置,则响应是关于多格式边界的错误。 我也对所需的“文件”参数感到困惑。我必须将它添加到 CURLOPT_POSTFIELDS 吗?

希望你们能帮助我谢谢!

编辑终稿

我明白了。以下代码适用于我提供的服务器设置。

std::string contents;
std::ifstream in("file.txt", std::ios::in | std::ios::binary);

if (in)
{
    in.seekg(0, std::ios::end);
    contents.resize(in.tellg());
    in.seekg(0, std::ios::beg);
    in.read(&contents[0], contents.size());
    in.close();
}
//##


CURL *curl;
CURLcode res;

struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] =  "Expect:";

curl_global_init(CURL_GLOBAL_ALL);

// set up the header
curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "cache-control:",
    CURLFORM_COPYCONTENTS, "no-cache",
    CURLFORM_END);

curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "content-type:",
    CURLFORM_COPYCONTENTS, "multipart/form-data",
    CURLFORM_END);

curl_formadd(&formpost, &lastptr,
    CURLFORM_COPYNAME, "file",  // <--- the (in this case) wanted file-Tag!
    CURLFORM_BUFFER, "data",
    CURLFORM_BUFFERPTR, contents.data(),
    CURLFORM_BUFFERLENGTH, contents.size(),
    CURLFORM_END);

curl = curl_easy_init();

headerlist = curl_slist_append(headerlist, buf);
if (curl) {

    curl_easy_setopt(curl, CURLOPT_URL, "url");

    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(curl);
    /* Check for errors */
    if (res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));


    curl_easy_cleanup(curl);


    curl_formfree(formpost);

    curl_slist_free_all(headerlist);
}

【问题讨论】:

  • 同样在这个例子中我得到了没有文件数据的消息。我所做的唯一更改是将“postit2.c”更改为“file.txt”。

标签: c++ curl


【解决方案1】:

您的多部分数据格式不正确。您可以在 netcat 实用程序的帮助下查看它的外观:

在 1 号航站楼:

$ nc -lk 8080 # listen for TCP connection on port 8080

在 2 号航站楼:

$ curl -X POST -F "file=@test.txt" localhost:8080

通过检查终端 1,您将看到 curl 发送了类似于以下内容的请求:

POST / HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 193
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------a975b09b8e15800c

--------------------------a975b09b8e15800c
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

qwerty

--------------------------a975b09b8e15800c--

现在,调试您的 C++ 代码,使其将数据发送到 netcat (localhost:8080) 并修复代码,直到其请求类似于 curl 发送的请求。

一般建议 - 不要盲目调试。您无法控制的服务器的响应没有提供有关问题的足够信息。如果可能,请在允许您查看数据的每一位并验证数据是否符合规范和/或您的期望的环境中进行调试。

【讨论】:

  • 好的,谢谢,但我认为我的技能太低了。我下载了 Windows 的 Netcat 并转到控制台并添加“nc -l 8080”(k 不存在)但它没有找到主机。
  • 另外,我不理解线程顶部的 cUrl 操作,它工作正常。并且没有设置边界或其他格式的文件。那我为什么要在代码里这么说呢?
  • @HEvTH curl 可执行文件自动添加边界和 Content-Disposition 数据。
  • @HEvTH nc 报告的错误的确切文本是什么?
  • "nc: 转发主机查找失败:h_errno 11001: HOST_NOT_FOUND"
【解决方案2】:

你的命令行使用的是POST,但你的代码使用的是CURLOPT_UPLOAD,这显然是不同的。

您应该改用CURLOPT_POST,并且应该将文件读入(或映射)到缓冲区中:

// Assume you read or map your file into 
// `uint8_t* buffer`, with `uint32_t size`
...
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_URL, "url");
curl_easy_setopt(hnd, CURLOPT_POST, 1);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, buffer);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE, size);

【讨论】:

  • 感谢您的回答。我试过了,现在卡在了另一个地方。我根据你的回答编辑我的帖子,希望你有时间看看,谢谢。
  • 尝试在CURLOPT_POSTFIELDS的内容中添加“file=”。当然,如果您的服务器需要额外的标头,您也应该添加 Cache-Control: no-cache 标头。
  • 谢谢,但我认为边界也有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多