C++ CURL post 文件然后得到响应错误

标签 c++ curl

我正在尝试将文件发布到 API,然后检查各种响应。但是每当我尝试发布大于 0 字节的文件时,我都会收到错误消息:

First-chance exception at 0x77AADF63 (ntdll.dll) in Test.exe: 
0xC0000005: Access violation writing location 0x00000014.

相关代码为:

std::string Network::Post(std::string url, std::map<std::string, std::string> requestBody, std::string file, int port) {
    std::ostringstream response;

    std::stringstream bodyStream;
    struct stat file_info;


    for (std::map<std::string, std::string>::iterator iterator = requestBody.begin(); iterator != requestBody.end(); iterator++) {
        bodyStream << iterator->first << "=" << iterator->second << "&";
    }

    //Get file
    FILE* file_handle = fopen(file.c_str(), "rb");
    int t = fstat(_fileno(file_handle), &file_info);

    CURL *ch;
    curl_global_init(CURL_GLOBAL_ALL);
    ch = curl_easy_init();

    struct curl_slist *headers = NULL;

    curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");

    std::string bodyStreamString = bodyStream.str();
    const char* bodyStreamCharPtr = bodyStreamString.c_str();

    curl_easy_setopt(ch, CURLOPT_URL, url.c_str());
    curl_easy_setopt(ch, CURLOPT_PORT, port);
    curl_easy_setopt(ch, CURLOPT_POSTFIELDS, bodyStreamCharPtr);
    curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, bodyStream.str().length());
    curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
    curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(ch, CURLOPT_COOKIEFILE, "cookies.dat");
    curl_easy_setopt(ch, CURLOPT_COOKIEJAR, "cookies.dat");
    curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, &ResponseToString);
    curl_easy_setopt(ch, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(ch, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt(ch, CURLOPT_READDATA, file_handle);
    curl_easy_setopt(ch, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
    curl_easy_setopt(ch, CURLOPT_WRITEDATA, &response);

    int res = curl_easy_perform(ch);
    curl_easy_cleanup(ch);

    return response.str();
}


size_t Network::ResponseToString(char *ptr, size_t size, size_t nmemb, void *stream) {
    std::ostringstream *s = (std::ostringstream*)stream;
    size_t count = size * nmemb;
    s->write(ptr, count);
    return count;
}

知道发生了什么事吗?卡了一天左右,感叹:(

更多详情:

它只是在“int res = curl_easy_perform(ch);”处中断,不能踏入其中。

如果我删除 WRITEFUNCTION 和 WRITEDATA 选项,它可以工作,但我无法得到响应。

问题似乎也不在 ResponseToString 方法中。

最佳答案

问题出在您的 WRITEFUNC 上。

我很确定 Network::ResponseToString 不是静态的,这会导致问题,因为非静态函数会将“this”作为第一个参数传递,然后搞砸你的函数参数,我建议避免使用类成员完全使用它(放置在与 Network::Post 相同的文件中并在 Post 方法定义之前):

static size_t ResponseToString(char *ptr, size_t size, size_t nmemb, std::ostringstream *stream) {
    size_t count = size * nmemb;
    stream->write(ptr, count);
    return count;
}

还记得更改:

curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, &ResponseToString);

在:

curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, ResponseToString);

此外,如果您的范围是一个简单的“post”,则不需要您在 curl opt 声明中指定的许多 header :

curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, bodyStream.str().length());
curl_easy_setopt(ch, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(ch, CURLOPT_READDATA, file_handle);
curl_easy_setopt(ch, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);

关于C++ CURL post 文件然后得到响应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32244887/

相关文章:

c++ - 如果我只给它一个指向我的对象的指针,std::vector 会占用什么内存?

php - 我的代码有什么问题( future 支付服务器端集成)

curl - 使用 PROPFIND 显示文件夹中文件的分块列表

c++ - 第一个包无法使用非阻塞 tcp 套接字发送

c++ - 什么是 MSVCP100D.dll?

c++ - 如何将 C++ 程序编译为 32 位可执行文件而不是 64 位

python - EOFError : EOF when reading a line only when execute it via curl 错误

curl - 使用 curl 连接到 docker

php - Laravel 家园 : How to fix 'cURL error 60: SSL certificate problem'

c++ - C++ lambda 表达式的生命周期是多少?