CHttpClient.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef CHTTPCLIENT_H
  2. #define CHTTPCLIENT_H
  3. #include <QString>
  4. #include <curl/curl.h>
  5. #include <vector>
  6. enum FORM_DATA_TYPE
  7. {
  8. fdt_content,
  9. fdt_file
  10. };
  11. class CHttpClient
  12. {
  13. public:
  14. CHttpClient();
  15. ~CHttpClient();
  16. void addParam(std::string sKey, std::string sValue);
  17. void addHeader(std::string sKey, std::string sValue);
  18. void addFormData(FORM_DATA_TYPE nType, std::string sKey, std::string sValue);
  19. void addFormDataList(FORM_DATA_TYPE nType, std::string sKey, std::vector<std::string> sValue);
  20. bool post(std::string uri, std::string &sResponse, int &nResCode);
  21. bool post(std::string uri, std::string sPostStr, std::string &sResponse, int &nResCode);
  22. bool postFormData(std::string uri, std::string &sResponse, int &nResCode);
  23. bool get(std::string uri, std::string &sResponse, int &nResCode);
  24. bool put(std::string uri, std::string &sResponse, int &nResCode);
  25. bool download(std::string uri, std::string sFileName, int &nResCode);
  26. void setTimeOut(int nTimeOut);
  27. private:
  28. static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm);
  29. std::string encodeStr(std::string sStr);
  30. std::string encodeURL(std::string sUri);
  31. void sslVerify();
  32. std::string m_sErrorMsg;
  33. CURL *m_pCurl;
  34. curl_slist *m_pList;
  35. curl_httppost *m_formpost;
  36. curl_httppost *m_lastptr;
  37. std::string m_sParmStr;
  38. int m_nTimeOutSecord;
  39. static std::once_flag oc_init;
  40. static std::once_flag oc_destory;
  41. };
  42. #endif // CHTTPCLIENT_H