如标题所示,我想将C++程序中带有cUrl lib的文件发送到Web服务器。

我从服务器获取了url,并获得了它想要HTTP POST方法以及在mutipart/form-data中的信息。另外,它还需要一个带有数据的参数文件。

我还收到了一个与curl控制台一起使用的cUrl调用:



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

现在我的代码:

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

现在服务器响应没有文件数据。我是否也要说这是multipart/form-data?但是,如果我取消注释标题设置,响应将是关于多格式边界的错误。
我对所需的"file"参数也感到困惑。我是否必须将其添加到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);
}

最佳答案

您没有正确格式化多部分数据。您可以在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发送的请求为止。

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

关于C++网址将多部分/表单数据文件发送到Web服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38320819/

10-13 04:46
查看更多