CurlClient.cpp 13 KB

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