|
@@ -57,7 +57,7 @@ static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
|
|
|
return nmemb;
|
|
return nmemb;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
|
|
|
|
|
|
|
+int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, CZhipuziHttpProxy proxy)
|
|
|
{
|
|
{
|
|
|
CURLcode res;
|
|
CURLcode res;
|
|
|
CURL* curl = curl_easy_init();
|
|
CURL* curl = curl_easy_init();
|
|
@@ -79,6 +79,19 @@ int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, s
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
|
|
+
|
|
|
|
|
+ if (proxy.m_is_proxy)
|
|
|
|
|
+ {
|
|
|
|
|
+ std::string addr = proxy.ip + ":" + proxy.port;
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXY, addr.c_str());
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
|
|
|
|
+
|
|
|
|
|
+ if (!proxy.username.empty() && !proxy.password.empty())
|
|
|
|
|
+ {
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, (proxy.username + ":" + proxy.password).c_str());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
|
|
@@ -87,7 +100,7 @@ int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, s
|
|
|
return res;
|
|
return res;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
|
|
|
|
|
|
|
+int CHttpClient::Get(const std::string & strUrl, std::string & strResponse, CZhipuziHttpProxy proxy)
|
|
|
{
|
|
{
|
|
|
CURLcode res;
|
|
CURLcode res;
|
|
|
CURL* curl = curl_easy_init();
|
|
CURL* curl = curl_easy_init();
|
|
@@ -107,6 +120,19 @@ int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
|
|
+
|
|
|
|
|
+ if (proxy.m_is_proxy)
|
|
|
|
|
+ {
|
|
|
|
|
+ std::string addr = proxy.ip + ":" + proxy.port;
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXY, addr.c_str());
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
|
|
|
|
+
|
|
|
|
|
+ if (!proxy.username.empty() && !proxy.password.empty())
|
|
|
|
|
+ {
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, (proxy.username + ":" + proxy.password).c_str());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
|
|
* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
|
|
|
* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
|
|
* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
|
|
@@ -122,7 +148,7 @@ int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
|
|
|
/*
|
|
/*
|
|
|
*返回值说明 https://blog.csdn.net/u011857683/article/details/53069268
|
|
*返回值说明 https://blog.csdn.net/u011857683/article/details/53069268
|
|
|
**/
|
|
**/
|
|
|
-int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath, bool is_http_proxy, std::string http_proxy_ip, std::string http_proxy_port)
|
|
|
|
|
|
|
+int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath, CZhipuziHttpProxy proxy)
|
|
|
{
|
|
{
|
|
|
CURLcode res;
|
|
CURLcode res;
|
|
|
CURL* curl = curl_easy_init();
|
|
CURL* curl = curl_easy_init();
|
|
@@ -147,12 +173,17 @@ int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost,
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
//curl_easy_setopt(curl, CURLOPT_COOKIE, "lwm_gray_tag=rc");;
|
|
//curl_easy_setopt(curl, CURLOPT_COOKIE, "lwm_gray_tag=rc");;
|
|
|
|
|
|
|
|
- if (is_http_proxy)
|
|
|
|
|
|
|
+ if (proxy.m_is_proxy)
|
|
|
{
|
|
{
|
|
|
- std::string addr = http_proxy_ip + ":" + http_proxy_port;
|
|
|
|
|
|
|
+ std::string addr = proxy.ip + ":" + proxy.port;
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXY, addr.c_str());
|
|
curl_easy_setopt(curl, CURLOPT_PROXY, addr.c_str());
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
|
|
curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
|
|
|
|
|
+
|
|
|
|
|
+ if (!proxy.username.empty() && !proxy.password.empty())
|
|
|
|
|
+ {
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, (proxy.username + ":" + proxy.password).c_str());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (NULL == pCaPath)
|
|
if (NULL == pCaPath)
|
|
@@ -176,7 +207,7 @@ int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost,
|
|
|
return res;
|
|
return res;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
|
|
|
|
|
|
|
+int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath, CZhipuziHttpProxy proxy)
|
|
|
{
|
|
{
|
|
|
CURLcode res;
|
|
CURLcode res;
|
|
|
CURL* curl = curl_easy_init();
|
|
CURL* curl = curl_easy_init();
|
|
@@ -198,6 +229,19 @@ int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, con
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
|
|
|
|
|
|
|
+ if (proxy.m_is_proxy)
|
|
|
|
|
+ {
|
|
|
|
|
+ std::string addr = proxy.ip + ":" + proxy.port;
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXY, addr.c_str());
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
|
|
|
|
|
+
|
|
|
|
|
+ if (!proxy.username.empty() && !proxy.password.empty())
|
|
|
|
|
+ {
|
|
|
|
|
+ curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, (proxy.username + ":" + proxy.password).c_str());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (NULL == pCaPath)
|
|
if (NULL == pCaPath)
|
|
|
{
|
|
{
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|