123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "http_client.h"
- #define HTTP_TIMEOUT_MSECS 120000
- string HttpClient::proxyIp;
- string HttpClient::proxyPort;
- string HttpClient::proxyUserName;
- string HttpClient::proxyPassword;
- HttpClient::HttpClient()
- {
- CURLcode init_ret = curl_global_init(CURL_GLOBAL_ALL);
- curl = curl_easy_init();
- responseEntity.clear();
- }
- HttpClient::~HttpClient() {
- curl_easy_cleanup(curl);
- curl_global_cleanup();
- }
- void HttpClient::setProxy(const string &proxyIp,
- const string &proxyPort,
- const string &proxyUserName,
- const string &proxyPassword) {
- HttpClient::proxyIp = proxyIp;
- HttpClient::proxyPort = proxyPort;
- HttpClient::proxyUserName = proxyUserName;
- HttpClient::proxyPassword = proxyPassword;
- }
- size_t HttpClient::write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
- int length = size * nmemb;
- HttpClient *client = (HttpClient *)stream;
- client->responseEntity = string((char *)ptr, length);
- return length;
- }
- string HttpClient::sendSyncRequest(const string &url,
- const string &requestEntity,
- const HeaderMap &headers) {
- if(curl == NULL) {
- DebugLog("%s | %s", url.c_str(), "curl easy init error");
- return string();
- }
- struct curl_slist * headerlist = NULL;
- string headitem;
- for (HeaderMap::const_iterator iter = headers.begin(); iter != headers.end(); ++iter) {
- headitem = iter->first;
- headitem += " : ";
- headitem += iter->second;
- headerlist = curl_slist_append(headerlist, headitem.c_str());
- headitem.clear();
- }
-
-
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_setopt(curl, CURLOPT_HEADER, 1);
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
- curl_easy_setopt(curl, CURLOPT_POST, 1);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestEntity.c_str());
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
- if (!proxyIp.empty() && !proxyPort.empty()) {
- string proxyStr = proxyIp;
- proxyStr += ":";
- proxyStr += proxyPort;
- curl_easy_setopt(curl, CURLOPT_PROXY, proxyStr.c_str());
- if (!proxyUserName.empty() && !proxyPassword.empty()) {
- string proxyUserPwd = proxyUserName;
- proxyUserPwd += ":";
- proxyUserPwd += proxyPassword;
- curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxyUserPwd.c_str());
- }
- }
- DebugLog("send:%s", requestEntity.c_str());
- responseEntity.clear();
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
- curl_easy_perform(curl);
- curl_slist_free_all(headerlist);
- DebugLog("resv:%s", responseEntity.c_str());
- return responseEntity;
- }
- string HttpClient::sendSyncRequest(const string &url,
- const ParamsMap ¶msMap,
- const HeaderMap &headers) {
- string requestEntity;
- string item;
- for (ParamsMap::const_iterator iter = paramsMap.begin(); iter != paramsMap.end(); ++iter) {
- const char *key = iter->first.c_str();
- char *encodedKey = curl_easy_escape(curl, key, strlen(key));
- if (encodedKey) {
- item = encodedKey;
- }
- item += "=";
- const char *value = iter->second.c_str();
- char *encodedValue = curl_easy_escape(curl, value, strlen(value));
- if (encodedValue) {
- item += encodedValue;
- }
- if (!requestEntity.empty()) {
- requestEntity.push_back('&');
- }
- requestEntity.append(item);
- item.clear();
- if (encodedKey) {
- curl_free(encodedKey);
- }
- if (encodedValue) {
- curl_free(encodedValue);
- }
- }
- string responseStr = sendSyncRequest(url, requestEntity, headers);
- return responseStr;
- }
|