CurlClient.cpp 12 KB

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