CurlClient.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. #include "pch.h"
  2. #include "CurlClient.h"
  3. #include "CharEncoding.h"
  4. CCurlClient::CCurlClient(void)
  5. {
  6. m_bDebug = FALSE;
  7. m_headers = NULL;
  8. }
  9. CCurlClient::~CCurlClient(void)
  10. {
  11. // 释放curl的全局对象;
  12. curl_global_cleanup();
  13. }
  14. INT CCurlClient::Initialize()
  15. {
  16. // 初始化全局调用模式;
  17. CURLcode res = ::curl_global_init( CURL_GLOBAL_ALL );
  18. if( CURLE_OK != res )
  19. {
  20. fprintf( stderr, "curl_global_init failed: %d \n", res );
  21. return -1;
  22. }
  23. return 0;
  24. }
  25. static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
  26. {
  27. if(itype == CURLINFO_TEXT)
  28. {
  29. //TRACE("[TEXT]%s\n", pData);
  30. LOG4C((LOG_WARN, "[TEXT]%s\n", pData));
  31. }
  32. else if(itype == CURLINFO_HEADER_IN)
  33. {
  34. //TRACE("[HEADER_IN]%s\n", pData);
  35. LOG4C((LOG_WARN, "[HEADER_IN]%s\n", pData));
  36. }
  37. else if(itype == CURLINFO_HEADER_OUT)
  38. {
  39. //TRACE("[HEADER_OUT]%s\n", pData);
  40. LOG4C((LOG_WARN, "[HEADER_OUT]%s\n", pData));
  41. }
  42. else if(itype == CURLINFO_DATA_IN)
  43. {
  44. //TRACE("[DATA_IN]%s\n", pData);
  45. LOG4C((LOG_WARN, "[DATA_IN]%s\n", pData));
  46. }
  47. else if(itype == CURLINFO_DATA_OUT)
  48. {
  49. //TRACE("[DATA_OUT]%s\n", pData);
  50. LOG4C((LOG_WARN, "[DATA_OUT]%s\n", pData));
  51. }
  52. return 0;
  53. }
  54. size_t CCurlClient::OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream)
  55. {
  56. if( NULL == stream || NULL == ptr )
  57. return -1;
  58. stream->append((char*)ptr, size * nmemb);
  59. return nmemb;
  60. }
  61. int CCurlClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, long time_out /*= 3*/)
  62. {
  63. CURLcode res;
  64. CURL* curl = curl_easy_init();
  65. if(NULL == curl)
  66. {
  67. return CURLE_FAILED_INIT;
  68. }
  69. if(m_bDebug)
  70. {// 是否开启调试日志输出;
  71. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  72. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  73. }
  74. // 设置URL地址;
  75. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  76. // 设置POST方式;
  77. curl_easy_setopt(curl, CURLOPT_POST, 1);
  78. // 设置POST参数;
  79. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
  80. // 设置回调函数-读取;
  81. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  82. // 设置回调函数-写入;
  83. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  84. // 设置回调函数-写入的缓存区;
  85. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  86. // 设置(多线程下,只是尽量减少)无签名;
  87. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  88. // 设置连接超时值(单位毫秒);
  89. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30000);
  90. // 设置操作超时值(单位毫秒);
  91. curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
  92. // 设置头;
  93. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  94. // 执行POST提交;
  95. res = curl_easy_perform(curl);
  96. // 释放资源;
  97. curl_easy_cleanup(curl);
  98. //curl_global_cleanup();
  99. ClearHeaders(); /* free the header list */
  100. return res;
  101. }
  102. int CCurlClient::Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out /*= 3*/)
  103. {
  104. if ( lpUrl == NULL || lpPost == NULL )
  105. return CURLE_FAILED_INIT;
  106. string strUrl;
  107. string strPost;
  108. string strResponse;
  109. #ifdef UNICODE
  110. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  111. CharEncoding::UNICODE2ASCII((LPWCH)lpPost, strPost);
  112. int res = Post(strUrl, strPost, strResponse) ;
  113. if ( CURLE_OK == res )
  114. {
  115. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  116. return CURLE_OK;
  117. }
  118. return res;
  119. #else
  120. strUrl = lpUrl;
  121. strPost = lpPost;
  122. int res = Post(strUrl, strPost, strResponse, time_out) ;
  123. if ( CURLE_OK == res )
  124. {
  125. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  126. return CURLE_OK;
  127. }
  128. return res;
  129. #endif
  130. }
  131. int CCurlClient::Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, long time_out /*= 3*/)
  132. {
  133. if ( strUrl.IsEmpty() || strPost.IsEmpty() )
  134. return CURLE_FAILED_INIT;
  135. string url;
  136. string post;
  137. string response;
  138. #ifdef UNICODE
  139. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  140. CharEncoding::UNICODE2ASCII((LPWCH)strPost.GetString(), post);
  141. int res = Post(url, post, response) ;
  142. if ( CURLE_OK == res )
  143. {
  144. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  145. if ( pResult )
  146. {
  147. strResponse = pResult;
  148. delete []pResult;
  149. pResult = NULL;
  150. return CURLE_OK;
  151. }
  152. }
  153. return res;
  154. #else
  155. url = strUrl.GetString();
  156. post = strPost.GetString();
  157. int res = Post(url, post, response, time_out) ;
  158. if ( CURLE_OK == res )
  159. {
  160. strResponse = response.c_str();
  161. return CURLE_OK;
  162. }
  163. return res;
  164. #endif
  165. }
  166. int CCurlClient::Get(const std::string & strUrl, std::string & strResponse, long time_out /*= 3*/)
  167. {
  168. CURLcode res;
  169. CURL* curl = curl_easy_init();
  170. if(NULL == curl)
  171. {
  172. return CURLE_FAILED_INIT;
  173. }
  174. if(m_bDebug)
  175. {
  176. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  177. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  178. }
  179. // 设置URL地址;
  180. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  181. // 设置回调函数-读取;
  182. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  183. // 设置回调函数-写入;
  184. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  185. // 设置回调函数-写入的缓存区;
  186. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  187. /**
  188. * 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
  189. * 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
  190. */
  191. // 设置(多线程下,只是尽量减少)无签名;
  192. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  193. // 设置连接超时值;
  194. curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5000);
  195. // 设置超时值;
  196. curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
  197. // 设置头;
  198. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  199. res = curl_easy_perform(curl);
  200. curl_easy_cleanup(curl);
  201. //curl_global_cleanup();
  202. ClearHeaders(); /* free the header list */
  203. return res;
  204. }
  205. int CCurlClient::Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out /*= 3*/)
  206. {
  207. if ( lpUrl == NULL )
  208. return CURLE_FAILED_INIT;
  209. string strUrl;
  210. string strResponse;
  211. #ifdef UNICODE
  212. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  213. int res = Get(strUrl, strResponse) ;
  214. if ( CURLE_OK == res )
  215. {
  216. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  217. return CURLE_OK;
  218. }
  219. return res;
  220. #else
  221. strUrl = lpUrl;
  222. int res = Get(strUrl, strResponse, time_out) ;
  223. if ( CURLE_OK == res )
  224. {
  225. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  226. return CURLE_OK;
  227. }
  228. return res;
  229. #endif
  230. }
  231. int CCurlClient::Get(IN CString& strUrl, OUT CString& strResponse, long time_out /*= 3*/)
  232. {
  233. if ( strUrl.IsEmpty() )
  234. return CURLE_FAILED_INIT;
  235. string url;
  236. string post;
  237. string response;
  238. #ifdef UNICODE
  239. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  240. int res = Get(url, response) ;
  241. if ( CURLE_OK == res )
  242. {
  243. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  244. if ( pResult )
  245. {
  246. strResponse = pResult;
  247. delete []pResult;
  248. pResult = NULL;
  249. return CURLE_OK;
  250. }
  251. }
  252. return res;
  253. #else
  254. url = strUrl.GetString();
  255. int res = Get(url, response, time_out) ;
  256. if ( CURLE_OK == res )
  257. {
  258. strResponse = response.c_str();
  259. return CURLE_OK;
  260. }
  261. return res;
  262. #endif
  263. }
  264. int CCurlClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath, long time_out /*= 3*/)
  265. {
  266. CURLcode res;
  267. CURL* curl = curl_easy_init();
  268. if(NULL == curl)
  269. {
  270. return CURLE_FAILED_INIT;
  271. }
  272. if(m_bDebug)
  273. {
  274. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  275. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  276. }
  277. // 设置URL地址;
  278. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  279. // 设置POST提交方式;
  280. curl_easy_setopt(curl, CURLOPT_POST, 1);
  281. // 设置POST参数;
  282. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
  283. // 设置回调函数-读取;
  284. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  285. // 设置回调函数-写入;
  286. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  287. // 设置回调函数-写入的缓存区;
  288. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  289. // 设置;
  290. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  291. if(NULL == pCaPath || pCaPath[0] == '\0')
  292. {
  293. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  294. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  295. }
  296. else
  297. {
  298. //缺省情况就是PEM,所以无需设置,另外支持DER
  299. //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
  300. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  301. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  302. }
  303. // 设置连接超时值;
  304. curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5000);
  305. // 设置超时值;
  306. curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
  307. // 设置头;
  308. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  309. // 执行POST提交;
  310. res = curl_easy_perform(curl);
  311. // 释放资源;
  312. curl_easy_cleanup(curl);
  313. //curl_global_cleanup();
  314. ClearHeaders(); /* free the header list */
  315. return res;
  316. }
  317. int CCurlClient::Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath /* = NULL */, long time_out /*= 3*/)
  318. {
  319. if ( lpUrl == NULL || lpPost == NULL )
  320. return CURLE_FAILED_INIT;
  321. string strUrl;
  322. string strPost;
  323. string strCapath;
  324. string strResponse;
  325. #ifdef UNICODE
  326. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  327. CharEncoding::UNICODE2ASCII((LPWCH)lpPost, strPost);
  328. CharEncoding::UNICODE2ASCII((LPWCH)lpCaPath, strCapath);
  329. int res = Posts(strUrl, strPost, strResponse, strCapath.c_str()) ;
  330. if ( CURLE_OK == res )
  331. {
  332. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  333. return CURLE_OK;
  334. }
  335. return res;
  336. #else
  337. strUrl = lpUrl;
  338. strPost = lpPost;
  339. strCapath = lpCaPath;
  340. int res = Posts(strUrl, strPost, strResponse, strCapath.c_str(), time_out ) ;
  341. if ( CURLE_OK == res )
  342. {
  343. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  344. return CURLE_OK;
  345. }
  346. return res;
  347. #endif
  348. }
  349. int CCurlClient::Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath /* = _T("") */, long time_out /*= 3*/)
  350. {
  351. if ( strUrl.IsEmpty() || strPost.IsEmpty() )
  352. return CURLE_FAILED_INIT;
  353. string url;
  354. string post;
  355. string capth;
  356. string response;
  357. #ifdef UNICODE
  358. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  359. CharEncoding::UNICODE2ASCII((LPWCH)strPost.GetString(), post);
  360. CharEncoding::UNICODE2ASCII((LPWCH)strCaPath.GetString(), capth);
  361. int res = Posts(url, post, response, capth.c_str()) ;
  362. if ( CURLE_OK == res )
  363. {
  364. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  365. if ( pResult )
  366. {
  367. strResponse = pResult;
  368. delete []pResult;
  369. pResult = NULL;
  370. return CURLE_OK;
  371. }
  372. }
  373. return res;
  374. #else
  375. url = strUrl.GetString();
  376. post = strPost.GetString();
  377. capth = strCaPath.GetString();
  378. int res = Posts(url, post, response, capth.c_str(), time_out) ;
  379. if ( CURLE_OK == res )
  380. {
  381. strResponse = response.c_str();
  382. return CURLE_OK;
  383. }
  384. return res;
  385. #endif
  386. }
  387. int CCurlClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath, long time_out /*= 3*/)
  388. {
  389. CURLcode res;
  390. CURL* curl = curl_easy_init();
  391. if(NULL == curl)
  392. {
  393. return CURLE_FAILED_INIT;
  394. }
  395. if(m_bDebug)
  396. {
  397. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  398. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  399. }
  400. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  401. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  402. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  403. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  404. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  405. if(NULL == pCaPath || pCaPath[0] == '\0')
  406. {
  407. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  408. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  409. }
  410. else
  411. {
  412. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  413. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  414. }
  415. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5000);
  416. curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
  417. // 设置头;
  418. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  419. res = curl_easy_perform(curl);
  420. curl_easy_cleanup(curl);
  421. //curl_global_cleanup();
  422. ClearHeaders(); /* free the header list */
  423. return res;
  424. }
  425. int CCurlClient::Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath /* = NULL */, long time_out /*= 3*/)
  426. {
  427. if ( lpUrl == NULL )
  428. return CURLE_FAILED_INIT;
  429. string strUrl;
  430. string strCapath;
  431. string strResponse;
  432. #ifdef UNICODE
  433. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  434. CharEncoding::UNICODE2ASCII((LPWCH)lpCaPath, strCapath);
  435. int res = Gets(strUrl, strResponse, strCapath.c_str()) ;
  436. if ( CURLE_OK == res )
  437. {
  438. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  439. return CURLE_OK;
  440. }
  441. return res;
  442. #else
  443. strUrl = lpUrl;
  444. strCapath = lpCaPath;
  445. int res = Gets(strUrl, strResponse, strCapath.c_str(), time_out) ;
  446. if ( CURLE_OK == res )
  447. {
  448. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  449. return CURLE_OK;
  450. }
  451. return res;
  452. #endif
  453. }
  454. int CCurlClient::Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath /* = _T("") */, long time_out /*= 3*/)
  455. {
  456. if ( strUrl.IsEmpty() )
  457. return CURLE_FAILED_INIT;
  458. string url;
  459. string post;
  460. string capth;
  461. string response;
  462. #ifdef UNICODE
  463. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  464. CharEncoding::UNICODE2ASCII((LPWCH)strCaPath.GetString(), capth);
  465. int res = Gets(url, response, capth.c_str()) ;
  466. if ( CURLE_OK == res )
  467. {
  468. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  469. if ( pResult )
  470. {
  471. strResponse = pResult;
  472. delete []pResult;
  473. pResult = NULL;
  474. return CURLE_OK;
  475. }
  476. }
  477. return res;
  478. #else
  479. url = strUrl.GetString();
  480. capth = strCaPath.GetString();
  481. int res = Gets(url, response, capth.c_str(), time_out) ;
  482. if ( CURLE_OK == res )
  483. {
  484. strResponse = response.c_str();
  485. return CURLE_OK;
  486. }
  487. return res;
  488. #endif
  489. }
  490. void CCurlClient::SetDebug(bool bDebug)
  491. {
  492. m_bDebug = bDebug;
  493. }
  494. void CCurlClient::SetHeaders(const std::string headers)
  495. {
  496. m_headers = curl_slist_append(m_headers, headers.c_str());
  497. }