c/c++ 实现curl http地址

21 篇文章 0 订阅
16 篇文章 0 订阅


其实如果这种实现,个人还是推荐脚本来搞,毕竟脚本方便些, 这里用c/c++,只是代码较方便,从之前写的项目中涉及到的一个curl部分


有管用什么实现,写出来只是希望大家需要注意: 业务层,通信层, 协议层(这里我先这么区别,为的是和下面的代码对应方便来说)

例如下面的代码中,标红色的地方表示针对业务层上的通信已完成(仅是通信完成,这里并不代表成功与否)

而绿色我把这描述成协议层,即通信完成情况下,协议层是否完成,


我举这个例子目的不是想说c/c++对应的curl如何来写,只是想表达程序我理解分三个层,第一是外部的业务逻辑,然后是通信层,最后是协议层

再例如一个需求,你把数据用protobuf封好后通过网络传输到另端的库中存储起来,那么这个过程可以描述为: 通信层通过rpc调用传输这个protobuf数据(此时通信层完成传输任务,他不管你到底这个protobuf入没入到库中),然后入到库中后反馈状态至业务层,业务层只有在通信与协议均正常下才为业务正常,否则表示需求没有达到要求




size_t WriteData(char *ptr, size_t size, size_t nmemb, void *out) {
((std::stringstream*)out)->write(ptr, size * nmemb);
return size * nmemb;
}


int Curl(std::string &url , std::map<std::string, std::string>&__map,) {
int ret_code = -1;
long httpcode = 0;
std::stringstream out;
string response = "";
//curl_global_init(CURL_GLOBAL_ALL);//declared in main()
CURL *easy_handle = curl_easy_init();
if (easy_handle == NULL) {
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
return FAIL;
}
curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, 10);
curl_easy_setopt(easy_handle, CURLOPT_NOSIGNAL, 1L);
//curl_easy_setopt(easy_handle, CURLOPT_FORBID_REUSE, 1);
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, WriteData);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &out);
curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(easy_handle, CURLOPT_HTTPGET,1L);


int ret_url = curl_easy_perform(easy_handle);
if(CURLE_COULDNT_CONNECT == ret_url || CURLE_OK != ret_url){
return FAIL;
}



curl_easy_getinfo( easy_handle, CURLINFO_RESPONSE_CODE, &httpcode ); 
if( 200 == httpcode  ) {
response = out.str();

if(response.size() > 0) {
if(0){
;//ret_code = /*call function*/
}else{
ret_code = OK;
}
} else {
ret_code = ERROR_BUF_SIZE; // const int ERROR_BUF_SIZE  = -3;
}
} else {

ret_code = FAIL;
}


curl_easy_cleanup(easy_handle);
curl_global_cleanup();


return ret_code;
}

在C/C++中,可以使用不同的库来实现HTTP客户端功能。以下是两个常用的库和示例代码: 1. 使用libcurl实现C/C++HTTP客户端功能: ```c #include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); res = curl_easy_perform(curl); if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } ``` 2. 使用Boost库中的Beast模块实现C++HTTP客户端功能: ```cpp #include <iostream> #include <boost/beast/core.hpp> #include <boost/beast/http.hpp> #include <boost/beast/version.hpp> #include <boost/asio/connect.hpp> #include <boost/asio/ip/tcp.hpp> namespace beast = boost::beast; namespace http = beast::http; namespace net = boost::asio; using tcp = net::ip::tcp; int main() { try { net::io_context io_context; tcp::resolver resolver(io_context); tcp::resolver::results_type endpoints = resolver.resolve("example.com", "80"); tcp::socket socket(io_context); net::connect(socket, endpoints); http::request<http::string_body> req{http::verb::get, "/", 11}; req.set(http::field::host, "example.com"); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); http::write(socket, req); beast::flat_buffer buffer; http::response<http::dynamic_body> res; http::read(socket, buffer, res); std::cout << res << std::endl; beast::error_code ec; socket.shutdown(tcp::socket::shutdown_both, ec); if(ec && ec != beast::errc::not_connected) throw beast::system_error{ec}; } catch(const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return 1; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值