使用libcurl的包装库cpr发起http请求
Posted buyishi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用libcurl的包装库cpr发起http请求相关的知识,希望对你有一定的参考价值。
cpr GitHub地址https://github.com/whoshuu/cpr
简单示例:cpr_http_request.cpp
#include <iostream> #include <cpr/cpr.h> int main() { std::string requestAddress("https://api.eyekey.com/face/Check/checking"); std::string requestParams("app_id=f89ae61fd63d4a63842277e9144a6bd2&app_key=af1cd33549c54b27ae24aeb041865da2&url=http%3A%2F%2Fpicview01.baomihua.com%2Fphotos%2F20120713%2Fm_14_634778197959062500_40614445.jpg"); std::cout << "GET Sample:" << std::endl; const cpr::Response &responseForGet = cpr::Get(cpr::Url(requestAddress.append("?" + requestParams))); std::cout << ‘ ‘ << responseForGet.status_code << std::endl; std::cout << ‘ ‘ << responseForGet.text << std::endl; std::cout << "POST Sample:" << std::endl; cpr::Multipart multipart{ { "app_id", "f89ae61fd63d4a63842277e9144a6bd2" }, { "app_key", "af1cd33549c54b27ae24aeb041865da2" }, {"url", "https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=18bb0cc2dd1373f0f53f68999c342cc6/caef76094b36acafe725024570d98d1000e99c46.jpg"} }; const cpr::Response &responseForPost = cpr::Post(cpr::Url(requestAddress), multipart); std::cout << ‘ ‘ << responseForPost.status_code << std::endl; std::cout << ‘ ‘ << responseForPost.text << std::endl; std::system("pause"); return 0; }
以上是关于使用libcurl的包装库cpr发起http请求的主要内容,如果未能解决你的问题,请参考以下文章
C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)
libcurl发起post请求时间延迟问题。except为空即可
基于 Boost.ASIO 的 HTTP 客户端库(如 libcurl)[关闭]