CurlClient.cpp 13 KB

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