CurlClient.cpp 17 KB

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