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#include <kurlyk.hpp>
6
20std::unordered_map<std::string, std::string> read_proxy_config(const std::string& filename) {
21 std::unordered_map<std::string, std::string> config;
22 std::ifstream file(filename);
23 std::string line;
24
25 while (std::getline(file, line)) {
26 std::istringstream iss(line);
27 std::string key, value;
28 if (std::getline(iss, key, '=') && std::getline(iss, value)) {
29 config[key] = value;
30 }
31 }
32 return config;
33}
34
37 << "ready: " << response->ready << std::endl
38 << "response: " << std::endl
39 << response->content << std::endl
40 << "error_code: " << response->error_code << std::endl
41 << "status_code: " << response->status_code << std::endl
42 << "----------------------------------------" << std::endl;
43}
44
45int main() {
46 kurlyk::init(true);
47 kurlyk::HttpClient client("https://httpbin.org");
48
49 // Reading proxy parameters from configuration file
50 const std::string config_filename = "proxy_config.txt";
51 auto proxy_config = read_proxy_config(config_filename);
52
53 // Setting proxy parameters if found in the configuration file
54 if (proxy_config.find("proxy_ip") != proxy_config.end() &&
55 proxy_config.find("proxy_port") != proxy_config.end()) {
56
57 std::string proxy_ip = proxy_config["proxy_ip"];
58 int proxy_port = std::stoi(proxy_config["proxy_port"]);
59 std::string proxy_username = proxy_config["proxy_username"];
60 std::string proxy_password = proxy_config["proxy_password"];
61
62 client.set_proxy(proxy_ip, proxy_port, proxy_username, proxy_password, kurlyk::ProxyType::HTTP);
63 }
64
65 client.set_user_agent("KurlykClient/1.0");
66 client.set_timeout(10); // Setting request timeout in seconds
67 client.set_connect_timeout(5); // Setting connection timeout in seconds
68 client.set_retry_attempts(3, 1000); // Setting retries with delay of 1000 ms
69
70 // Sending GET request using HttpClient
71 KURLYK_PRINT << "Sending GET request using HttpClient..." << std::endl;
72 client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers(),
73 [](const kurlyk::HttpResponsePtr response) {
74 print_response(response);
75 });
76
77 KURLYK_PRINT << "Press Enter to exit..." << std::endl;
78 std::cin.get();
79 client.cancel_requests();
81 return 0;
82}
A client class for making HTTP 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 the active request associated with this client and waits for its completion.
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
std::unique_ptr< HttpResponse > HttpResponsePtr
A unique pointer to an HttpResponse object for memory management.
void deinit()
Deinitializes the Kurlyk library, stopping the network worker and releasing resources.
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