首页 > 解决方案 > Libcurl Returns Corrupt Data

问题描述

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

Above are all my opts for the transfer. The download returns a .tar.gz file which using command line curl or WGET returns absolutely fine, using libcurl the returned data is about 8k short, doing a cmp shows that the 2 files change within the first 10 bytes. Been looking through this for 2 days now with no sign of hope. Tried various different combinations of opts.

s_Write_Data is implemented as:

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


}

The response header I get from 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

标签: c++curllibcurl

解决方案


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

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


推荐阅读