CurlClient.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. #include "stdafx.h"
  2. #include "CurlClient.h"
  3. #include "CharEncoding.h"
  4. #include <sys/stat.h>
  5. CCurlClient::CCurlClient(void)
  6. {
  7. #ifdef _DEBUG
  8. m_bDebug = TRUE;
  9. #else
  10. m_bDebug = FALSE;
  11. #endif
  12. m_headers = NULL;
  13. }
  14. CCurlClient::~CCurlClient(void)
  15. {
  16. // 释放curl的全局对象;
  17. ClearHeaders();
  18. curl_global_cleanup();
  19. }
  20. INT CCurlClient::Initialize()
  21. {
  22. // 初始化全局调用模式;
  23. CURLcode res = ::curl_global_init( CURL_GLOBAL_ALL );
  24. if( CURLE_OK != res )
  25. {
  26. fprintf( stderr, "curl_global_init failed: %d \n", res );
  27. return -1;
  28. }
  29. return 0;
  30. }
  31. static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
  32. {
  33. if(itype == CURLINFO_TEXT)
  34. {
  35. TRACE("[TEXT]%s\n", pData);
  36. //LOG4C((LOG_WARN, "[TEXT]%s\n", pData));
  37. }
  38. else if(itype == CURLINFO_HEADER_IN)
  39. {
  40. TRACE("[HEADER_IN]%s\n", pData);
  41. //LOG4C((LOG_WARN, "[HEADER_IN]%s\n", pData));
  42. }
  43. else if(itype == CURLINFO_HEADER_OUT)
  44. {
  45. TRACE("[HEADER_OUT]%s\n", pData);
  46. //LOG4C((LOG_WARN, "[HEADER_OUT]%s\n", pData));
  47. }
  48. else if(itype == CURLINFO_DATA_IN)
  49. {
  50. TRACE("[DATA_IN]%s\n", pData);
  51. //LOG4C((LOG_WARN, "[DATA_IN]%s\n", pData));
  52. }
  53. else if(itype == CURLINFO_DATA_OUT)
  54. {
  55. TRACE("[DATA_OUT]%s\n", pData);
  56. //LOG4C((LOG_WARN, "[DATA_OUT]%s\n", pData));
  57. }
  58. return 0;
  59. }
  60. size_t CCurlClient::OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream)
  61. {
  62. if( NULL == stream || NULL == ptr )
  63. return -1;
  64. stream->append((char*)ptr, size * nmemb);
  65. return nmemb;
  66. }
  67. size_t CCurlClient::OnWriteFile(const void *ptr, size_t size, size_t nmemb, void *stream)
  68. {
  69. if( NULL == stream || NULL == ptr )
  70. return -1;
  71. return fwrite(ptr, size, nmemb, (FILE*)stream);
  72. }
  73. size_t CCurlClient::OnGetContentLength(void *ptr, size_t size, size_t nmemb, void *stream)
  74. {
  75. int r;
  76. long len = 0;
  77. /* _snscanf() is Win32 specific */
  78. // r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
  79. r = sscanf_s((char*)ptr, "Content-Length: %ld\n", &len);
  80. if (r) /* Microsoft: we don't read the specs */
  81. *((long *) stream) = len;
  82. return size * nmemb;
  83. }
  84. int CCurlClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, long time_out /*= 3*/)
  85. {
  86. CURLcode res;
  87. CURL* curl = curl_easy_init();
  88. if(NULL == curl)
  89. {
  90. return CURLE_FAILED_INIT;
  91. }
  92. if(m_bDebug)
  93. {// 是否开启调试日志输出;
  94. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  95. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  96. }
  97. // 设置URL地址;
  98. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  99. // 设置POST方式;
  100. curl_easy_setopt(curl, CURLOPT_POST, 1);
  101. // 设置POST参数;
  102. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
  103. // 设置回调函数-读取;
  104. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  105. // 设置回调函数-写入;
  106. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  107. // 设置回调函数-写入的缓存区;
  108. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  109. // 设置(多线程下,只是尽量减少)无签名;
  110. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  111. // 设置连接超时值(单位毫秒);
  112. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30000);
  113. // 设置操作超时值(单位毫秒);
  114. curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
  115. // 设置头;
  116. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  117. // 执行POST提交;
  118. res = curl_easy_perform(curl);
  119. // 释放资源;
  120. curl_easy_cleanup(curl);
  121. //curl_global_cleanup();
  122. ClearHeaders(); /* free the header list */
  123. return res;
  124. }
  125. int CCurlClient::Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out /*= 3*/)
  126. {
  127. if ( lpUrl == NULL || lpPost == NULL )
  128. return CURLE_FAILED_INIT;
  129. string strUrl;
  130. string strPost;
  131. string strResponse;
  132. #ifdef UNICODE
  133. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  134. CharEncoding::UNICODE2ASCII((LPWCH)lpPost, strPost);
  135. int res = Post(strUrl, strPost, strResponse) ;
  136. if ( CURLE_OK == res )
  137. {
  138. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  139. return CURLE_OK;
  140. }
  141. return res;
  142. #else
  143. strUrl = lpUrl;
  144. strPost = lpPost;
  145. int res = Post(strUrl, strPost, strResponse, time_out) ;
  146. if ( CURLE_OK == res )
  147. {
  148. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  149. return CURLE_OK;
  150. }
  151. return res;
  152. #endif
  153. }
  154. int CCurlClient::Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, long time_out /*= 3*/)
  155. {
  156. if ( strUrl.IsEmpty() || strPost.IsEmpty() )
  157. return CURLE_FAILED_INIT;
  158. string url;
  159. string post;
  160. string response;
  161. #ifdef UNICODE
  162. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  163. CharEncoding::UNICODE2ASCII((LPWCH)strPost.GetString(), post);
  164. int res = Post(url, post, response) ;
  165. if ( CURLE_OK == res )
  166. {
  167. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  168. if ( pResult )
  169. {
  170. strResponse = pResult;
  171. delete []pResult;
  172. pResult = NULL;
  173. return CURLE_OK;
  174. }
  175. }
  176. return res;
  177. #else
  178. url = strUrl.GetString();
  179. post = strPost.GetString();
  180. int res = Post(url, post, response, time_out) ;
  181. if ( CURLE_OK == res )
  182. {
  183. strResponse = response.c_str();
  184. return CURLE_OK;
  185. }
  186. return res;
  187. #endif
  188. }
  189. int CCurlClient::Get(const std::string & strUrl, std::string & strResponse, long time_out /*= 3*/)
  190. {
  191. CURLcode res;
  192. CURL* curl = curl_easy_init();
  193. if(NULL == curl)
  194. {
  195. return CURLE_FAILED_INIT;
  196. }
  197. if(m_bDebug)
  198. {
  199. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  200. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  201. }
  202. // 设置URL地址;
  203. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  204. // 设置回调函数-读取;
  205. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  206. // 设置回调函数-写入;
  207. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  208. // 设置回调函数-写入的缓存区;
  209. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  210. /**
  211. * 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
  212. * 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
  213. */
  214. // 设置(多线程下,只是尽量减少)无签名;
  215. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  216. // 设置连接超时值;
  217. curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5000);
  218. // 设置超时值;
  219. curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
  220. // 设置头;
  221. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  222. res = curl_easy_perform(curl);
  223. curl_easy_cleanup(curl);
  224. //curl_global_cleanup();
  225. ClearHeaders(); /* free the header list */
  226. return res;
  227. }
  228. int CCurlClient::Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out /*= 3*/)
  229. {
  230. if ( lpUrl == NULL )
  231. return CURLE_FAILED_INIT;
  232. string strUrl;
  233. string strResponse;
  234. #ifdef UNICODE
  235. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  236. int res = Get(strUrl, strResponse) ;
  237. if ( CURLE_OK == res )
  238. {
  239. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  240. return CURLE_OK;
  241. }
  242. return res;
  243. #else
  244. strUrl = lpUrl;
  245. int res = Get(strUrl, strResponse, time_out) ;
  246. if ( CURLE_OK == res )
  247. {
  248. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  249. return CURLE_OK;
  250. }
  251. return res;
  252. #endif
  253. }
  254. int CCurlClient::Get(IN CString& strUrl, OUT CString& strResponse, long time_out /*= 3*/)
  255. {
  256. if ( strUrl.IsEmpty() )
  257. return CURLE_FAILED_INIT;
  258. string url;
  259. string post;
  260. string response;
  261. #ifdef UNICODE
  262. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  263. int res = Get(url, response) ;
  264. if ( CURLE_OK == res )
  265. {
  266. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  267. if ( pResult )
  268. {
  269. strResponse = pResult;
  270. delete []pResult;
  271. pResult = NULL;
  272. return CURLE_OK;
  273. }
  274. }
  275. return res;
  276. #else
  277. url = strUrl.GetString();
  278. int res = Get(url, response, time_out) ;
  279. if ( CURLE_OK == res )
  280. {
  281. strResponse = response.c_str();
  282. return CURLE_OK;
  283. }
  284. return res;
  285. #endif
  286. }
  287. int CCurlClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath, long time_out /*= 3*/)
  288. {
  289. CURLcode res;
  290. CURL* curl = curl_easy_init();
  291. if(NULL == curl)
  292. {
  293. return CURLE_FAILED_INIT;
  294. }
  295. if(m_bDebug)
  296. {
  297. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  298. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  299. }
  300. // 设置URL地址;
  301. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  302. // 设置POST提交方式;
  303. curl_easy_setopt(curl, CURLOPT_POST, 1);
  304. // 设置POST参数;
  305. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
  306. // 设置回调函数-读取;
  307. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  308. // 设置回调函数-写入;
  309. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  310. // 设置回调函数-写入的缓存区;
  311. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  312. // 设置;
  313. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  314. if(NULL == pCaPath || pCaPath[0] == '\0')
  315. {
  316. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  317. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  318. }
  319. else
  320. {
  321. //缺省情况就是PEM,所以无需设置,另外支持DER
  322. //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
  323. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  324. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  325. }
  326. // 设置连接超时值;
  327. curl_easy_setopt(curl,CURLOPT_CONNECTTIMEOUT,5000);
  328. // 设置超时值;
  329. curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
  330. // 设置头;
  331. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  332. // 执行POST提交;
  333. res = curl_easy_perform(curl);
  334. // 释放资源;
  335. curl_easy_cleanup(curl);
  336. //curl_global_cleanup();
  337. ClearHeaders(); /* free the header list */
  338. return res;
  339. }
  340. int CCurlClient::Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath /* = NULL */, long time_out /*= 3*/)
  341. {
  342. if ( lpUrl == NULL || lpPost == NULL )
  343. return CURLE_FAILED_INIT;
  344. string strUrl;
  345. string strPost;
  346. string strCapath;
  347. string strResponse;
  348. #ifdef UNICODE
  349. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  350. CharEncoding::UNICODE2ASCII((LPWCH)lpPost, strPost);
  351. CharEncoding::UNICODE2ASCII((LPWCH)lpCaPath, strCapath);
  352. int res = Posts(strUrl, strPost, strResponse, strCapath.c_str()) ;
  353. if ( CURLE_OK == res )
  354. {
  355. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  356. return CURLE_OK;
  357. }
  358. return res;
  359. #else
  360. strUrl = lpUrl;
  361. strPost = lpPost;
  362. strCapath = lpCaPath;
  363. int res = Posts(strUrl, strPost, strResponse, strCapath.c_str(), time_out ) ;
  364. if ( CURLE_OK == res )
  365. {
  366. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  367. return CURLE_OK;
  368. }
  369. return res;
  370. #endif
  371. }
  372. int CCurlClient::Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath /* = _T("") */, long time_out /*= 3*/)
  373. {
  374. if ( strUrl.IsEmpty() || strPost.IsEmpty() )
  375. return CURLE_FAILED_INIT;
  376. string url;
  377. string post;
  378. string capth;
  379. string response;
  380. #ifdef UNICODE
  381. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  382. CharEncoding::UNICODE2ASCII((LPWCH)strPost.GetString(), post);
  383. CharEncoding::UNICODE2ASCII((LPWCH)strCaPath.GetString(), capth);
  384. int res = Posts(url, post, response, capth.c_str()) ;
  385. if ( CURLE_OK == res )
  386. {
  387. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  388. if ( pResult )
  389. {
  390. strResponse = pResult;
  391. delete []pResult;
  392. pResult = NULL;
  393. return CURLE_OK;
  394. }
  395. }
  396. return res;
  397. #else
  398. url = strUrl.GetString();
  399. post = strPost.GetString();
  400. capth = strCaPath.GetString();
  401. int res = Posts(url, post, response, capth.c_str(), time_out) ;
  402. if ( CURLE_OK == res )
  403. {
  404. strResponse = response.c_str();
  405. return CURLE_OK;
  406. }
  407. return res;
  408. #endif
  409. }
  410. int CCurlClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath, long time_out /*= 3*/)
  411. {
  412. CURLcode res;
  413. CURL* curl = curl_easy_init();
  414. if(NULL == curl)
  415. {
  416. return CURLE_FAILED_INIT;
  417. }
  418. if(m_bDebug)
  419. {
  420. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  421. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  422. }
  423. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  424. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  425. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  426. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  427. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  428. if(NULL == pCaPath || pCaPath[0] == '\0')
  429. {
  430. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  431. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  432. }
  433. else
  434. {
  435. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  436. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  437. }
  438. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5000);
  439. curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);
  440. // 设置头;
  441. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  442. res = curl_easy_perform(curl);
  443. curl_easy_cleanup(curl);
  444. //curl_global_cleanup();
  445. ClearHeaders(); /* free the header list */
  446. return res;
  447. }
  448. int CCurlClient::Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath /* = NULL */, long time_out /*= 3*/)
  449. {
  450. if ( lpUrl == NULL )
  451. return CURLE_FAILED_INIT;
  452. string strUrl;
  453. string strCapath;
  454. string strResponse;
  455. #ifdef UNICODE
  456. CharEncoding::UNICODE2ASCII((LPWCH)lpUrl, strUrl);
  457. CharEncoding::UNICODE2ASCII((LPWCH)lpCaPath, strCapath);
  458. int res = Gets(strUrl, strResponse, strCapath.c_str()) ;
  459. if ( CURLE_OK == res )
  460. {
  461. CharEncoding::ASCII2UNICODE(strResponse.c_str(), (LPWCH)lpResponse, nMaxlen);
  462. return CURLE_OK;
  463. }
  464. return res;
  465. #else
  466. strUrl = lpUrl;
  467. strCapath = lpCaPath;
  468. int res = Gets(strUrl, strResponse, strCapath.c_str(), time_out) ;
  469. if ( CURLE_OK == res )
  470. {
  471. sprintf_s(lpResponse, nMaxlen, "%s", strResponse.c_str());
  472. return CURLE_OK;
  473. }
  474. return res;
  475. #endif
  476. }
  477. int CCurlClient::Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath /* = _T("") */, long time_out /*= 3*/)
  478. {
  479. if ( strUrl.IsEmpty() )
  480. return CURLE_FAILED_INIT;
  481. string url;
  482. string post;
  483. string capth;
  484. string response;
  485. #ifdef UNICODE
  486. CharEncoding::UNICODE2ASCII((LPWCH)strUrl.GetString(), url);
  487. CharEncoding::UNICODE2ASCII((LPWCH)strCaPath.GetString(), capth);
  488. int res = Gets(url, response, capth.c_str()) ;
  489. if ( CURLE_OK == res )
  490. {
  491. WCHAR* pResult = CharEncoding::ASCII2UNICODE(response.c_str());
  492. if ( pResult )
  493. {
  494. strResponse = pResult;
  495. delete []pResult;
  496. pResult = NULL;
  497. return CURLE_OK;
  498. }
  499. }
  500. return res;
  501. #else
  502. url = strUrl.GetString();
  503. capth = strCaPath.GetString();
  504. int res = Gets(url, response, capth.c_str(), time_out) ;
  505. if ( CURLE_OK == res )
  506. {
  507. strResponse = response.c_str();
  508. return CURLE_OK;
  509. }
  510. return res;
  511. #endif
  512. }
  513. bool CCurlClient::Download(const std::string &url, const std::string &path, long time_out /* = 3000 */)
  514. {
  515. int nStatus = 0;
  516. std::string data;
  517. int npos = url.find("https://");
  518. if ( npos == std::string::npos)
  519. nStatus = Get(url, data, time_out);
  520. else
  521. nStatus = Gets(url, data, NULL, time_out);
  522. if ( nStatus != CURLE_OK)
  523. return false;
  524. if ( data.size() )
  525. {
  526. FILE *pf = NULL;
  527. fopen_s(&pf, path.c_str(), "wb");
  528. if ( pf )
  529. {
  530. fwrite(data.c_str(), data.size(), 1, pf);
  531. fclose(pf);
  532. return true;
  533. }
  534. }
  535. return false;
  536. }
  537. // 参考:https://blog.csdn.net/lengyuezuixue/article/details/81987695
  538. bool CCurlClient::DownloadEx(const std::string &url, const std::string &path, long time_out /* = 3000 */)
  539. {
  540. FILE *pf = NULL;
  541. curl_off_t local_file_len = -1;
  542. long file_size = 0;
  543. CURLcode res = CURLE_GOT_NOTHING;
  544. int c = 0;
  545. struct stat file_info;
  546. int use_resume = 0;
  547. // 获取本地文件大小;
  548. if (stat(path.c_str(), &file_info) == 0 )
  549. {
  550. local_file_len = file_info.st_size;
  551. use_resume = 1;
  552. }
  553. // 采用追加方式打开文件,便于实现断点续传;
  554. int nErr = fopen_s(&pf, path.c_str(), "ab+");
  555. if ( pf )
  556. {
  557. CURL* curl = curl_easy_init();
  558. if(NULL == curl)
  559. {
  560. //return CURLE_FAILED_INIT;
  561. return false;
  562. }
  563. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  564. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, time_out);
  565. // 设置http头部处理函数;
  566. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, OnGetContentLength);
  567. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &file_size);
  568. if ( url.find("https://") != std::string::npos )
  569. {
  570. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  571. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  572. }
  573. // 设置文件续传的位置给curl;
  574. curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, use_resume ? local_file_len : 0);
  575. curl_easy_setopt(curl, CURLOPT_WRITEDATA, pf);
  576. //curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);//设置重定位URL,使用自动跳转,返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据
  577. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteFile);
  578. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
  579. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  580. res = curl_easy_perform(curl);
  581. fclose(pf);
  582. curl_easy_cleanup(curl);
  583. if ( res == CURLE_OK )
  584. return true;
  585. }
  586. return false;
  587. }
  588. // 表单提交;
  589. // 注意与普通的post提交区别在于:CURLOPT_POST、CURLOPT_HTTPPOST
  590. int CCurlClient::FormPost(std::string url, std::multimap<std::string, std::string> form_data, std::string &result, long time_out)
  591. {
  592. CURLcode res;
  593. CURL* curl = curl_easy_init();
  594. if(NULL == curl)
  595. {
  596. return CURLE_FAILED_INIT;
  597. }
  598. if(m_bDebug)
  599. {
  600. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  601. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  602. }
  603. // 表单参数;
  604. std::string post;
  605. CURLFORMcode rmcode;
  606. struct curl_httppost* formpost = NULL;
  607. struct curl_httppost* lastptr = NULL;
  608. std::multimap<std::string, std::string>::iterator it = form_data.begin();
  609. #if 0
  610. for (; it != form_data.end(); )
  611. {
  612. post.append(it->first);
  613. post.append("=");
  614. post.append(it->second);
  615. if ( ++it != form_data.end() )
  616. post.append("&");
  617. else
  618. break;
  619. }
  620. // 设置POST参数;
  621. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
  622. // 设置POST提交方式;
  623. res = curl_easy_setopt(curl, CURLOPT_POST, 1);
  624. #else
  625. for (; it != form_data.end(); it++)
  626. {
  627. rmcode = curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, it->second.c_str(), CURLFORM_END);
  628. }
  629. // 设置表单参数
  630. res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  631. // 设置表头,表头内容可能不同
  632. //m_headers = curl_slist_append(m_headers, "Content-Type:multipart/form-data");
  633. //m_headers = curl_slist_append(m_headers, "Expect:");
  634. //m_headers = curl_slist_append(m_headers, "Accept-Encoding:gzip, deflate");//Accept-Encodeing冒号后面加东西就上传失败;
  635. //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  636. #endif
  637. // 设置URL地址;
  638. res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  639. // 设置回调函数-读取;
  640. res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  641. // 设置回调函数-写入;
  642. res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  643. // 设置回调函数-写入的缓存区;
  644. res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&result);
  645. // 设置;
  646. res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  647. //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  648. //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  649. curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置
  650. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据
  651. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
  652. curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 3 );//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
  653. // 设置超时值;
  654. res = curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
  655. // 设置头;
  656. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  657. // 执行POST提交;
  658. res = curl_easy_perform(curl);
  659. // 释放资源;
  660. curl_easy_cleanup(curl);
  661. //curl_global_cleanup();
  662. ClearHeaders(); /* free the header list */
  663. // 释放表单
  664. curl_formfree(formpost);
  665. return res;
  666. }
  667. int CCurlClient::FormPosts(std::string url, std::multimap<std::string, std::string> form_data, std::string &result, long time_out)
  668. {
  669. CURLcode res;
  670. CURL* curl = curl_easy_init();
  671. if(NULL == curl)
  672. {
  673. return CURLE_FAILED_INIT;
  674. }
  675. if(m_bDebug)
  676. {
  677. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  678. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  679. }
  680. // 表单参数;
  681. std::string post;
  682. CURLFORMcode rmcode;
  683. struct curl_httppost* formpost = NULL;
  684. struct curl_httppost* lastptr = NULL;
  685. std::multimap<std::string, std::string>::iterator it = form_data.begin();
  686. #if 0
  687. for (; it != form_data.end(); )
  688. {
  689. post.append(it->first);
  690. post.append("=");
  691. post.append(it->second);
  692. if ( ++it != form_data.end() )
  693. post.append("&");
  694. else
  695. break;
  696. }
  697. // 设置POST参数;
  698. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post.c_str());
  699. // 设置POST提交方式;
  700. res = curl_easy_setopt(curl, CURLOPT_POST, 1);
  701. #else
  702. for (; it != form_data.end(); it++)
  703. {
  704. rmcode = curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, it->second.c_str(), CURLFORM_END);
  705. }
  706. // 设置表单参数
  707. res = curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  708. // 设置表头,表头内容可能不同
  709. //m_headers = curl_slist_append(m_headers, "Content-Type:multipart/form-data");
  710. //m_headers = curl_slist_append(m_headers, "Expect:");
  711. //m_headers = curl_slist_append(m_headers, "Accept-Encoding:gzip, deflate");//Accept-Encodeing冒号后面加东西就上传失败;
  712. //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  713. #endif
  714. // 设置URL地址;
  715. res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  716. // 设置回调函数-读取;
  717. res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  718. // 设置回调函数-写入;
  719. res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  720. // 设置回调函数-写入的缓存区;
  721. res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&result);
  722. // 设置;
  723. res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  724. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  725. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  726. curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置
  727. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据
  728. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
  729. curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 30 );//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
  730. // 设置超时值;
  731. res = curl_easy_setopt(curl,CURLOPT_TIMEOUT, time_out);
  732. // 设置头;
  733. res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, m_headers);
  734. // 执行POST提交;
  735. res = curl_easy_perform(curl);
  736. // 释放资源;
  737. curl_easy_cleanup(curl);
  738. //curl_global_cleanup();
  739. ClearHeaders(); /* free the header list */
  740. // 释放表单
  741. curl_formfree(formpost);
  742. return res;
  743. }
  744. void CCurlClient::SetDebug(bool bDebug)
  745. {
  746. m_bDebug = bDebug;
  747. }
  748. void CCurlClient::SetHeaders(const std::string headers)
  749. {
  750. m_headers = curl_slist_append(m_headers, headers.c_str());
  751. }