我尝试更多时间并在谷歌搜索,但没有示例显示如何修改标题数据并使用Curl将其添加到下载。这是我的方法获取服务器的头数据响应
bool getHeader(CURL* url, std::string name, std::string url_down,
std::string savepath) {
bool result = false;
url = curl_easy_init();
FILE* headf;
CURLcode res;
std::string header_path = savepath + "/header.txt";
const char* url_d = url_down.c_str();
const char* save_header_down = header_path.c_str();
if (url) {
headf = fopen(save_header_down, "wb");
if (!headf) {
curl_easy_cleanup(url);
}
curl_easy_setopt(url, CURLOPT_URL, url_d);
curl_easy_setopt(url, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(url, CURLOPT_NOPROGRESS, false);
curl_easy_setopt(url, CURLOPT_NOBODY, true);
curl_easy_setopt(url, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(url, CURLOPT_HEADERDATA, headf);
res = curl_easy_perform(url);
LOGI("SDK: START DOWNLOAD HEADER");
if (res == CURLE_OK) {
LOGI("SDK: HEADER DOWNLOADED!");
char* ct;
char* ar;
double *dTotal;
res = curl_easy_getinfo(url, CURLINFO_CONTENT_TYPE, &ct);
res = curl_easy_getinfo(url, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dTotal);
res = curl_easy_getinfo(url, CURLINFO_ACCEPT_RANGES, &ar);
if((CURLE_OK == res) && ct){
LOGI("SDK: HEADER CONTENT TYPE: %s", ct);
LOGI("SDK: HEADER Content-Length: %d", dTotal);
LOGI("SDK: HEADER Accept-Ranges: %s", ar);
}
result = true;
}
fclose(headf);
curl_easy_cleanup(url);
}
return result;
}
你可以看到我在curl文档中找不到CURLINFO_ACCEPT_RANGES this 任何人都可以告诉我如何在卷曲中获得头部的接受范围,修改它并添加下载。感谢
答案 0 :(得分:0)
我不完全确定您尝试使用您的函数实现了什么,但这里是我如何从HTTP请求捕获数据和标头的示例。也许它会帮助你实现你想要做的事情:
#include <curl/curl.h>
size_t write_func(void* buf, size_t size, size_t nmemb, void* userp)
{
if(userp)
{
std::string& s = *static_cast<std::string*>(userp);
std::string::size_type len = size * nmemb;
s.append(static_cast<char*>(buf), len);
return nmemb;
}
return 0;
}
int main()
{
CURL* curl = curl_easy_init();
CURLcode code = CURLE_OK;
std::string url = "http://google.com/search?q=wibble";
long timeout = 60; // seconds
std::string data;
std::string headers;
if(CURLE_OK != (code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_func))
|| CURLE_OK != (code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L))
|| CURLE_OK != (code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L))
|| CURLE_OK != (code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data))
|| CURLE_OK != (code = curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &headers))
|| CURLE_OK != (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout))
|| CURLE_OK != (code = curl_easy_setopt(curl, CURLOPT_URL, url.c_str())))
{
std::cerr << curl_easy_strerror(code) << '\n';
return 1;
}
if(CURLE_OK != (code = curl_easy_perform(curl)))
{
std::cerr << curl_easy_strerror(code) << '\n';
return 1;
}
std::cout << "Data:" << '\n';
std::cout << data << '\n';
std::cout << "Headers:" << '\n';
std::cout << headers << '\n';
curl_easy_cleanup(curl);
}