-
Notifications
You must be signed in to change notification settings - Fork 29
Recipe: Configuring EasyHttp
Himanshu Shekhar edited this page Oct 4, 2017
·
1 revision
Applications can configure EasyHttp in a variety of ways to suit their needs. Here are some examples.
EasyHttp allows configuring connect/read/write timeouts in a single api. Currently these cannot be set individually.
try {
// configure http timeout
easyhttpcpp::EasyHttp::Builder httpClientBuilder;
httpClientBuilder.setTimeoutSec(30);
// create http client
easyhttpcpp::EasyHttp::Ptr pHttpClient = httpClientBuilder.build();
// create a new request and execute synchronously
easyhttpcpp::Request::Builder requestBuilder;
easyhttpcpp::Request::Ptr pRequest = requestBuilder.setUrl("http://httpbin.org/delay/2").build(); //This URL is served with a 2 second delay.
easyhttpcpp::Call::Ptr pCall = pHttpClient->newCall(pRequest);
easyhttpcpp::Response::Ptr pResponse = pCall->execute();
if (!pResponse->isSuccessful()) {
std::cout << "HTTP GET Error: (" << pResponse->getCode() << ")" << std::endl;
} else {
std::cout << "HTTP GET Success!" << std::endl;
}
} catch (const std::exception& e) {
std::cout << "Error occurred: " << e.what() << std::endl;
}
EasyHttp allows configuring network proxy. Currently, proxies requiring username and password are not supported.
try {
// configure network proxy
easyhttpcpp::Proxy::Ptr pProxy = new easyhttpcpp::Proxy("mycompany.proxy.com", 12345);
easyhttpcpp::EasyHttp::Builder httpClientBuilder;
httpClientBuilder.setProxy(pProxy);
// create http client
easyhttpcpp::EasyHttp::Ptr pHttpClient = httpClientBuilder.build();
// create a new request and execute synchronously
easyhttpcpp::Request::Builder requestBuilder;
easyhttpcpp::Request::Ptr pRequest = requestBuilder.setUrl("https://github.com/sony/easyhttpcpp").build();
easyhttpcpp::Call::Ptr pCall = pHttpClient->newCall(pRequest);
easyhttpcpp::Response::Ptr pResponse = pCall->execute();
if (!pResponse->isSuccessful()) {
std::cout << "HTTP GET Error: (" << pResponse->getCode() << ")" << std::endl;
} else {
std::cout << "HTTP GET Success!" << std::endl;
}
} catch (const std::exception& e) {
std::cout << "Error occurred: " << e.what() << std::endl;
}
Check EasyHttp::Builder
class inside EasyHttp.h
for a complete list of all the supported configurations. Setting configurations can be chained like, httpClientBuilder.setTimeoutSec(30).setProxy(pProxy).build()
and so on.
Feel free to open new issue if you want to ask any questions.