C++CURL无法设置标头
C++ CURL cannot set header
我目前正在编写一个关于CURL的程序。我已经写了以下代码来添加自定义标题:-
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "Another: yes");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
上面的代码很好,但如果我把代码改为下面的代码,我发现发送的标头不包含Another: yes
:
void add_header(CURL *c, struct curl_slist *h){
h = curl_slist_append(h, "Another: yes");
}
struct curl_slist *chunk = NULL;
add_header(curl, chunk);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
我的第二段代码有什么问题?
问题是将指向chunk
的指针传递给函数,然后为其分配不同的值。通过复制传递指针本身,这意味着函数内的h
,是一个不同于其外部chunk
的指针(是的,它们都指向同一位置,但这无关紧要,因为您更改的是指针本身的值,而不是它指向的内存)。要改变这一点,请通过引用传递指针:
void add_header(CURL *c, struct curl_slist *&h){ //note the *&
h = curl_slist_append(h, "Another: yes");
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 在C++中使用cURL和多线程
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何在24位SDL_Surface上设置像素的颜色
- std::设置自定义比较器
- 如何在 curl 代码C++设置授权持有者标头?我得到的授权不足,即使它在命令行上工作
- HTTP 标头未在 C++ 中使用 Curl 设置
- C++CURL无法设置标头
- CURLOPT_COOKIELIST可以在不同的curl easy句柄上设置吗?