|
@@ -5,7 +5,11 @@
|
|
|
|
|
|
CCurlClient::CCurlClient(void)
|
|
|
{
|
|
|
+#ifdef _DEBUG
|
|
|
+ m_bDebug = TRUE;
|
|
|
+#else
|
|
|
m_bDebug = FALSE;
|
|
|
+#endif
|
|
|
m_headers = NULL;
|
|
|
}
|
|
|
|
|
@@ -33,27 +37,27 @@ static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void
|
|
|
{
|
|
|
if(itype == CURLINFO_TEXT)
|
|
|
{
|
|
|
- //TRACE("[TEXT]%s\n", pData);
|
|
|
+ TRACE("[TEXT]%s\n", pData);
|
|
|
//LOG4C((LOG_WARN, "[TEXT]%s\n", pData));
|
|
|
}
|
|
|
else if(itype == CURLINFO_HEADER_IN)
|
|
|
{
|
|
|
- //TRACE("[HEADER_IN]%s\n", pData);
|
|
|
+ TRACE("[HEADER_IN]%s\n", pData);
|
|
|
//LOG4C((LOG_WARN, "[HEADER_IN]%s\n", pData));
|
|
|
}
|
|
|
else if(itype == CURLINFO_HEADER_OUT)
|
|
|
{
|
|
|
- //TRACE("[HEADER_OUT]%s\n", pData);
|
|
|
+ TRACE("[HEADER_OUT]%s\n", pData);
|
|
|
//LOG4C((LOG_WARN, "[HEADER_OUT]%s\n", pData));
|
|
|
}
|
|
|
else if(itype == CURLINFO_DATA_IN)
|
|
|
{
|
|
|
- //TRACE("[DATA_IN]%s\n", pData);
|
|
|
+ TRACE("[DATA_IN]%s\n", pData);
|
|
|
//LOG4C((LOG_WARN, "[DATA_IN]%s\n", pData));
|
|
|
}
|
|
|
else if(itype == CURLINFO_DATA_OUT)
|
|
|
{
|
|
|
- //TRACE("[DATA_OUT]%s\n", pData);
|
|
|
+ TRACE("[DATA_OUT]%s\n", pData);
|
|
|
//LOG4C((LOG_WARN, "[DATA_OUT]%s\n", pData));
|
|
|
}
|
|
|
return 0;
|
|
@@ -653,6 +657,182 @@ bool CCurlClient::DownloadEx(const std::string &url, const std::string &path, lo
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+// 表单提交;
|
|
|
+// 注意与普通的post提交区别在于:CURLOPT_POST、CURLOPT_HTTPPOST
|
|
|
+int CCurlClient::FormPost(std::string url, std::map<std::string, std::string> form_data, std::string &result, long time_out)
|
|
|
+{
|
|
|
+ CURLcode res;
|
|
|
+ CURL* curl = curl_easy_init();
|
|
|
+ if(NULL == curl)
|
|
|
+ {
|
|
|
+ return CURLE_FAILED_INIT;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(m_bDebug)
|
|
|
+ {
|
|
|
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
|
|
+ curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 表单参数;
|
|
|
+ std::string post;
|
|
|
+ CURLFORMcode rmcode;
|
|
|
+ struct curl_httppost* formpost = NULL;
|
|
|
+ struct curl_httppost* lastptr = NULL;
|
|
|
+ std::map<std::string, std::string>::iterator it = form_data.begin();
|
|
|
+#if 0
|
|
|
+ for (; it != form_data.end(); )
|
|
|
+ {
|
|
|
+ post.append(it->first);
|
|
|
+ post.append("=");
|
|
|
+ post.append(it->second);
|
|
|
+ if ( ++it != form_data.end() )
|
|
|
+ post.append("&");
|
|
|
+ else
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 设置POST参数;
|
|
|
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
|
|
|
+ // 设置POST提交方式;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|
|
+#else
|
|
|
+ for (; it != form_data.end(); it++)
|
|
|
+ {
|
|
|
+ rmcode = curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, it->second.c_str(), CURLFORM_END);
|
|
|
+ }
|
|
|
+ // 设置表单参数
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
|
|
+
|
|
|
+ // 设置表头,表头内容可能不同
|
|
|
+ //m_headers = curl_slist_append(m_headers, "Content-Type:multipart/form-data");
|
|
|
+ //m_headers = curl_slist_append(m_headers, "Expect:");
|
|
|
+ //m_headers = curl_slist_append(m_headers, "Accept-Encoding:gzip, deflate");//Accept-Encodeing冒号后面加东西就上传失败;
|
|
|
+ //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
|
|
|
+#endif
|
|
|
+
|
|
|
+ // 设置URL地址;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
+ // 设置回调函数-读取;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
+ // 设置回调函数-写入;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
+ // 设置回调函数-写入的缓存区;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&result);
|
|
|
+ // 设置;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
+
|
|
|
+ //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
+ curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置
|
|
|
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据
|
|
|
+ curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
|
|
|
+ curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 3 );//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
|
|
|
+
|
|
|
+ // 设置超时值;
|
|
|
+ res = curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
|
|
|
+ // 设置头;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
|
|
|
+
|
|
|
+ // 执行POST提交;
|
|
|
+ res = curl_easy_perform(curl);
|
|
|
+
|
|
|
+ // 释放资源;
|
|
|
+ curl_easy_cleanup(curl);
|
|
|
+ //curl_global_cleanup();
|
|
|
+ ClearHeaders(); /* free the header list */
|
|
|
+ // 释放表单
|
|
|
+ curl_formfree(formpost);
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
+
|
|
|
+int CCurlClient::FormPosts(std::string url, std::map<std::string, std::string> form_data, std::string &result, long time_out)
|
|
|
+{
|
|
|
+ CURLcode res;
|
|
|
+ CURL* curl = curl_easy_init();
|
|
|
+ if(NULL == curl)
|
|
|
+ {
|
|
|
+ return CURLE_FAILED_INIT;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(m_bDebug)
|
|
|
+ {
|
|
|
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
|
|
+ curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 表单参数;
|
|
|
+ std::string post;
|
|
|
+ CURLFORMcode rmcode;
|
|
|
+ struct curl_httppost* formpost = NULL;
|
|
|
+ struct curl_httppost* lastptr = NULL;
|
|
|
+ std::map<std::string, std::string>::iterator it = form_data.begin();
|
|
|
+#if 0
|
|
|
+ for (; it != form_data.end(); )
|
|
|
+ {
|
|
|
+ post.append(it->first);
|
|
|
+ post.append("=");
|
|
|
+ post.append(it->second);
|
|
|
+ if ( ++it != form_data.end() )
|
|
|
+ post.append("&");
|
|
|
+ else
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 设置POST参数;
|
|
|
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
|
|
|
+ // 设置POST提交方式;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|
|
+#else
|
|
|
+ for (; it != form_data.end(); it++)
|
|
|
+ {
|
|
|
+ rmcode = curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, it->second.c_str(), CURLFORM_END);
|
|
|
+ }
|
|
|
+ // 设置表单参数
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
|
|
+
|
|
|
+ // 设置表头,表头内容可能不同
|
|
|
+ //m_headers = curl_slist_append(m_headers, "Content-Type:multipart/form-data");
|
|
|
+ //m_headers = curl_slist_append(m_headers, "Expect:");
|
|
|
+ //m_headers = curl_slist_append(m_headers, "Accept-Encoding:gzip, deflate");//Accept-Encodeing冒号后面加东西就上传失败;
|
|
|
+ //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
|
|
|
+#endif
|
|
|
+
|
|
|
+ // 设置URL地址;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
+ // 设置回调函数-读取;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|
|
+ // 设置回调函数-写入;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
+ // 设置回调函数-写入的缓存区;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&result);
|
|
|
+ // 设置;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
|
|
+
|
|
|
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
+
|
|
|
+ curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置
|
|
|
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据
|
|
|
+ curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
|
|
|
+ curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 30 );//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
|
|
|
+
|
|
|
+ // 设置超时值;
|
|
|
+ res = curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
|
|
|
+ // 设置头;
|
|
|
+ res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
|
|
|
+
|
|
|
+ // 执行POST提交;
|
|
|
+ res = curl_easy_perform(curl);
|
|
|
+
|
|
|
+ // 释放资源;
|
|
|
+ curl_easy_cleanup(curl);
|
|
|
+ //curl_global_cleanup();
|
|
|
+ ClearHeaders(); /* free the header list */
|
|
|
+ // 释放表单
|
|
|
+ curl_formfree(formpost);
|
|
|
+
|
|
|
+ return res;
|
|
|
+}
|
|
|
|
|
|
void CCurlClient::SetDebug(bool bDebug)
|
|
|
{
|