libcurl第十四课: 获取返回报文的头部信息
2021/6/1 10:24:03
本文主要是介绍libcurl第十四课: 获取返回报文的头部信息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
场景
需要获取HTTP报头提取Cookie信息,发送给服务器,否则返回302重定向错误
static size_t Writeresponse(void *ptr, size_t size, size_t nmemb, void *userData) { string* pBuffer = (string*)userData; size_t length = size * nmemb; pBuffer->append((char*)ptr, length); return length; }
int CCS::LoginEx() { CURL *hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(hnd, CURLOPT_URL, "http://127.0.0.1:7000/proj/login"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Postman-Token: ec3ffce4-5c3c-4786-9396-578ff396c11d"); headers = curl_slist_append(headers, "cache-control: no-cache"); headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded"); curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "username=slny001&password=Hx%40kj%2319&loginType=2&undefined="); std::string strResponse; curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, Writeresponse);//设置回调函数 //curl_easy_setopt(pCurlHandle, CURLOPT_HEADER, 1);//保存HTTP头部信息到strResponseData curl_easy_setopt(hnd, CURLOPT_WRITEDATA, &strResponse);//设置回调函数的参数,获取反馈信息 curl_easy_setopt(hnd, CURLOPT_HEADERFUNCTION, Writeresponse);//设置回调函数:输出response headers string responseHeadBuffer; curl_easy_setopt(hnd, CURLOPT_HEADERDATA, &responseHeadBuffer);//设置回调函数参数 CURLcode ret = curl_easy_perform(hnd); if (0 == ret) { int nPosOfCookie = responseHeadBuffer.find("Cookie: ", 0); if (nPosOfCookie > 0) { int nPosOfEndCookie = responseHeadBuffer.find(";", nPosOfCookie); m_cookie = responseHeadBuffer.substr(nPosOfCookie + 7, nPosOfEndCookie - nPosOfCookie - 7); } } curl_slist_free_all(headers); curl_easy_cleanup(hnd); return 0; }
这篇关于libcurl第十四课: 获取返回报文的头部信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-03微信支付提示下单账户与支付账户不一致-icode9专业技术文章分享
- 2024-07-03微信支付提示订单号重复-icode9专业技术文章分享
- 2024-07-02微服务启动nacos注册上去了,但是一直没有收到请求-icode9专业技术文章分享
- 2024-07-02如何检查文件的编码格式-icode9专业技术文章分享
- 2024-07-02sublime 更改编码格式-icode9专业技术文章分享
- 2024-06-30uniAPP 实现全屏左右滚动滚动的效果-icode9专业技术文章分享
- 2024-06-30如何在本地使用授权或插件-icode9专业技术文章分享
- 2024-06-30伪静态规则配置方法汇总-icode9专业技术文章分享
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享