c++ - Libcurl 返回损坏的数据

标签 c++ curl libcurl

curl_easy_setopt(m_pCurl,   CURLOPT_URL, szURL);
curl_easy_setopt(m_pCurl,   CURLOPT_UPLOAD,0);
curl_easy_setopt(m_pCurl,   CURLOPT_WRITEFUNCTION, s_Write_Data);
curl_easy_setopt(m_pCurl,   CURLOPT_WRITEDATA, this);
curl_easy_setopt(m_pCurl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(m_pCurl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(m_pCurl, CURLOPT_PROGRESSFUNCTION, s_Progress);
curl_easy_setopt(m_pCurl, CURLOPT_PROGRESSDATA, this);
curl_easy_setopt(m_pCurl, CURLOPT_HTTP_CONTENT_DECODING, 0L);

以上是我对转移的所有选择。下载返回一个 .tar.gz 文件,使用命令行 curl 或 WGET 返回绝对正常,使用 libcurl 返回的数据大约短 8k,执行 cmp 显示这 2 个文件在前 10 个字节内发生变化。现在已经看了 2 天了,没有任何希望的迹象。尝试了各种不同的选项组合。

s_Write_Data 实现为:

size_t CCurlManager::s_Write_Data(void *buffer, size_t size, size_t nmemb, void *userp)
{

    CCurlManager *pThis = reinterpret_cast<CCurlManager *>(userp);
    return pThis->WriteData(buffer, size, nmemb);


}

我从 curl verbose 得到的响应头:

HTTP/1.1 200 OK

Date: Mon, 17 Dec 2018 13:56:39 GMT

Server: Apache/2.2.13 (Linux/SUSE)

X-Powered-By: PHP/5.4.20

Content-Length: 393800

Content-Disposition: attachment; filename="test.tar.gz"

Connection: close

Content-Type: application/octet-stream

最佳答案

这是 wxString (wxWidgets) 的一个问题,wxString(char*, int) 为最大 int 字符创建一个 char* 字符串,或者直到达到 null char,这在大多数其他 (char *, int) 仅读取 int 字符的字符串构造函数。

基本上,我在 WriteData 函数中创建的字符串在到达空字符时终止。

关于c++ - Libcurl 返回损坏的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53814295/

相关文章:

php - curl_exec 在我不想时打印结果

c - 将 C FILE 指针传递给 curl_easy_setopt 函数的正确方法

c++ - 处理 linux 上的库依赖

c++ - 在 C++ 中构建 libcurl 库,Noob 问题

c++ - 处理 TCP 故障的正确机制是什么?

c++ - WinRT C++ (Win10) 从 SoftwareBitmap/BitmapBuffer 访问字节

c++ - cUrl 命令 C++ 代码替换

c - 编译时 libcurl 错误(负值)

python - 返回对象指针 : 'Cannot convert ' Coordinate *' to Python object'

c++ - 为什么某些类型的字符串连接明显快于其他类型?