Kurlyk
Loading...
Searching...
No Matches
http_client_proxy_example.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include <unordered_map>
5#define KURLYK_AUTO_INIT 0
6#include <kurlyk.hpp>
7
21std::unordered_map<std::string, std::string> read_proxy_config(const std::string& filename) {
22 std::unordered_map<std::string, std::string> config;
23 std::ifstream file(filename);
24 std::string line;
25
26 while (std::getline(file, line)) {
27 std::istringstream iss(line);
28 std::string key, value;
29 if (std::getline(iss, key, '=') && std::getline(iss, value)) {
30 config[key] = value;
31 }
32 }
33 return config;
34}
35
38 << "ready: " << response->ready << std::endl
39 << "response: " << std::endl
40 << response->content << std::endl
41 << "error_code: " << response->error_code << std::endl
42 << "status_code: " << response->status_code << std::endl
43 << "----------------------------------------" << std::endl;
44}
45
46int main() {
47 kurlyk::init(true);
48 kurlyk::HttpClient client("https://httpbin.org");
49
50 // Reading proxy parameters from configuration file
51 const std::string config_filename = "proxy_config.txt";
52 auto proxy_config = read_proxy_config(config_filename);
53
54 // Setting proxy parameters if found in the configuration file
55 if (proxy_config.find("proxy_ip") != proxy_config.end() &&
56 proxy_config.find("proxy_port") != proxy_config.end()) {
57
58 std::string proxy_ip = proxy_config["proxy_ip"];
59 int proxy_port = std::stoi(proxy_config["proxy_port"]);
60 std::string proxy_username = proxy_config["proxy_username"];
61 std::string proxy_password = proxy_config["proxy_password"];
62
63 client.set_proxy(proxy_ip, proxy_port, proxy_username, proxy_password, kurlyk::ProxyType::PROXY_HTTP);
64 }
65
66 client.set_user_agent("KurlykClient/1.0");
67 client.set_timeout(10); // Setting request timeout in seconds
68 client.set_connect_timeout(5); // Setting connection timeout in seconds
69 client.set_retry_attempts(3, 1000); // Setting retries with delay of 1000 ms
70
71 // Sending GET request using HttpClient
72 KURLYK_PRINT << "Sending GET request using HttpClient..." << std::endl;
73 client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers(),
74 [](const kurlyk::HttpResponsePtr response) {
75 print_response(response);
76 });
77
78 KURLYK_PRINT << "Press Enter to exit..." << std::endl;
79 std::cin.get();
80 client.cancel_requests();
82 return 0;
83}
Concrete HTTP client for making requests to a specific host.
void set_proxy(const std::string &ip, int port, ProxyType type=ProxyType::PROXY_HTTP)
Sets the proxy server address.
void set_connect_timeout(long connect_timeout)
Sets the connection timeout duration.
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
void set_timeout(long timeout)
Sets the timeout duration for HTTP requests.
void set_user_agent(const std::string &user_agent)
Sets the User-Agent header.
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
void cancel_requests()
Cancels requests associated with this client and waits for cancellation callbacks.
void print_response(const kurlyk::HttpResponsePtr &response)
std::unordered_map< std::string, std::string > read_proxy_config(const std::string &filename)
Reads proxy configuration from a text file.
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition runtime.hpp:13
@ PROXY_HTTP
HTTP proxy.
Definition enums.hpp:13
std::unique_ptr< HttpResponse > HttpResponsePtr
Owning pointer to an HTTP response.
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition runtime.hpp:26
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT