123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- #include "RestfulFunc.h"
- #include <cpprest/http_client.h>
- #if defined(_MSC_VER)
- #include <Windows.h>
- #endif //_MSC_VER
- using namespace utility; // Common utilities like string conversions
- using namespace web; // Common features like URIs.
- using namespace web::http; // Common HTTP functionality
- using namespace web::http::client; // HTTP client features
- namespace
- {
- #if defined(_MSC_VER)
- static std::wstring s2w(const std::string str)
- {
- wchar_t* wstr = NULL;
- int n = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
- if (n > 0) {
- wstr = new wchar_t[n + 1];
- if (wstr == NULL) {
- return std::wstring();
- }
- std::memset(wstr, 0, (n + 1) * sizeof(wchar_t));
- ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wstr[0], n);
- std::wstring strr(wstr);
- delete wstr;
- return strr;
- }
- return std::wstring();
- }
- #endif //_MSC_VER
- std::string w2s(const std::wstring wstr)
- {
- #if defined(_MSC_VER)
- char* str = NULL;
- int n = ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
- if (n > 0) {
- str = new char[n + 1];
- if (str == NULL) {
- return std::string();
- }
- std::memset(str, 0, sizeof(char) * (n + 1));
- ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], n, NULL, NULL);
- std::string strr(str);
- delete str;
- return strr;
- }
- return std::string();
- #else
- if (wstr.empty()) {
- return "";
- }
- unsigned len = wcstombs(NULL, wstr.c_str(), 0) + 1;
- if (NULL == setlocale(LC_ALL, "zh_CN.gbk")) {
- }
- char* p = new char[len];
- wcstombs(p, wstr.c_str(), len);
- p[len - 1] = '\0';
- std::string str(p);
- delete[] p;
- return str;
- #endif // _MSC_VER
- }
- }
- #if defined(_MSC_VER)
- #define STRW(str) s2w(str)
- #define WSTR(str) w2s(str)
- #else
- #define STRW(str) (str)
- #define WSTR(str) (str)
- #endif //_MSC_VER
- RestfulClient& RestfulClient::getInstance()
- {
- static RestfulClient c;
- return c;
- }
- RestfulClient::RestfulClient()
- {
- }
- RestfulClient::~RestfulClient()
- {
- }
- namespace
- {
- const http::method MappingHttpRequestMethod(HttpRequestMethod provideMethod)
- {
- switch (provideMethod) {
- case GET:
- return http::methods::GET;
- break;
- case POST:
- return http::methods::POST;
- break;
- case PUT:
- return http::methods::PUT;
- break;
- case DEL:
- return http::methods::DEL;
- break;
- case HEAD:
- return http::methods::HEAD;
- break;
- case OPTIONS:
- return http::methods::OPTIONS;
- break;
- case TRCE:
- return http::methods::TRCE;
- break;
- case CONNECT:
- return http::methods::CONNECT;
- break;
- case MERGE:
- return http::methods::MERGE;
- break;
- case PATCH:
- return http::methods::PATCH;
- break;
- case UPLOAD:
- return http::methods::POST;
- break;
- case DOWNLOAD:
- return http::methods::POST;
- break;
- default:
- break;
- }
- return http::methods::GET;
- }
- void GetFileNameAndType(const std::string& absoluteFilePath, std::string& fileName, std::string& contentType)
- {
- size_t pos = absoluteFilePath.find_last_of("/\\");
- fileName = absoluteFilePath.substr(pos + 1);
- contentType = "application/octet-stream";
- pos = fileName.find_last_of(".");
- if (pos != std::string::npos) {
- std::string ext = fileName.substr(pos + 1);
- std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
- if (ext == "jpg" || ext == "jpeg") {
- contentType = "image/jpeg";
- } else if (ext == "txt" /*|| ext == "log"*/) {
- contentType = "text/plain";
- }
- }
- }
- }
- void RestfulClient::Do(const HttpClientRequestConfig* const pRequestConfig, HttpClientResponseResult* pResponse, HttpClientTraceLink* pTrace) const
- {
- http_client_config config;
- config.set_validate_certificates(pRequestConfig->NeedValidCert());
- const uint32_t timeoutVal = pRequestConfig->GetTimeout();
- if (timeoutVal != 0) {
- config.set_timeout(utility::seconds(timeoutVal));
- }
- web::http::client::http_client client(STRW(pRequestConfig->GetBaseUri()), config);
- http_request request(MappingHttpRequestMethod(pRequestConfig->GetRequestType()));
- uri_builder urib(STRW(pRequestConfig->GetSubUri()));
- if (pRequestConfig->GetQueryPairs().size() > 0) {
- const auto& queries = pRequestConfig->GetQueryPairs();
- for (auto it = queries.cbegin(); it != queries.cend(); ++it) {
- urib.append_query(STRW(it->first), STRW(it->second));
- }
- }
- request.set_request_uri(urib.to_string());
- request.headers().add(header_names::accept, STRW(pRequestConfig->GetAcceptType()));
- std::string token, channelId, terminalno, reserve1;
- if (pRequestConfig->getToken(channelId, token, terminalno, reserve1))
- {
- request.headers().add(STRW("channelId"), STRW(channelId));
- request.headers().add(STRW("token"), STRW(token));
- request.headers().add(STRW("terminalno"), STRW(terminalno));
- }
- if (pTrace != NULL
- && strlen(pTrace->X_B3_BusinessId) > 0
- && strlen(pTrace->X_B3_TraceId) > 0
- && strlen(pTrace->X_B3_SpanId) > 0
- && strlen(pTrace->X_B3_ParentSpanId) > 0
- && strlen(pTrace->X_B3_Timestamp) > 0)
- {
- request.headers().add(STRW("X_B3_BusinessId"), STRW(pTrace->X_B3_BusinessId));
- request.headers().add(STRW("X_B3_TraceId"), STRW(pTrace->X_B3_BusinessId));
- request.headers().add(STRW("X_B3_SpanId"), STRW(pTrace->X_B3_BusinessId));
- request.headers().add(STRW("X_B3_ParentSpanId"), STRW(pTrace->X_B3_BusinessId));
- request.headers().add(STRW("X_B3_Timestamp"), STRW(pTrace->X_B3_BusinessId));
- }
- if (pRequestConfig->GetRequestType() != HttpRequestMethod::GET) {
- request.set_body(STRW(pRequestConfig->GetBodyContent()), STRW(pRequestConfig->GetContentType()));
- }
- if (pRequestConfig->GetRequestType() == HttpRequestMethod::DOWNLOAD) {
- pplx::task<void> requestTask = client.request(request)
- .then([pResponse](http_response response) ->pplx::task<utility::string_t> {
- pResponse->statusCode = response.status_code();
- if (pResponse->ResponseOK()) {
- return response.extract_string();
- } else {
- std::cout << "response status result: " << response.status_code() << std::endl;
- return pplx::task_from_result(utility::string_t());
- }
- })
- .then([pResponse](pplx::task<utility::string_t> vt) {
- try {
- pResponse->content = WSTR(vt.get());
- } catch (const http_exception& ex) {
- pResponse->statusCode = -1;
- pResponse->content = ex.what();
- }
- });
- try {
- requestTask.wait();
- } catch (const std::exception& ex) {
- pResponse->statusCode = -1;
- pResponse->content = ex.what();
- }
- } else {
- pplx::task<void> requestTask = client.request(request)
- .then([pResponse](http_response response) ->pplx::task<json::value> {
- pResponse->statusCode = response.status_code();
- if (pResponse->ResponseOK()) {
- return response.extract_json();
- } else {
- std::cout << "response status result: " << response.status_code() << std::endl;
- return pplx::task_from_result(json::value());
- }
- })
- .then([pResponse](pplx::task<json::value> vt) {
- try {
- json::value const& v = vt.get();
- pResponse->content = WSTR(v.to_string());
- } catch (const http_exception& ex) {
- pResponse->statusCode = -1;
- pResponse->content = ex.what();
- }
- });
- try {
- requestTask.wait();
- } catch (const std::exception& ex) {
- pResponse->statusCode = -1;
- pResponse->content = ex.what();
- }
- }
- }
- std::pair<std::string, std::string> HttpClientUploadRequest::BuildBodyContent() const
- {
- std::stringstream data;
- std::string boundary{};
- for (int i = 0; i < 50; i++) {
- boundary += (rand() % 26) + 'A';
- }
- for (auto& param : mParams) {
- data << "\r\n--";
- data << boundary;
- data << "\r\nContent-Disposition: form-data; name=\"";
- data << param.first;
- data << "\"\r\n\r\n";
- data << param.second;
- }
- for (auto& file : mFiles) {
- std::string inputFileName;
- std::string contentType;
- GetFileNameAndType(file.second, inputFileName, contentType);
- std::ifstream inputFile;
- inputFile.open(file.second, std::ios::binary | std::ios::in);
- std::string inputFileContent = std::string((std::istreambuf_iterator<char>(inputFile)), (std::istreambuf_iterator<char>()));
- data << "\r\n--";
- data << boundary;
- data << "\r\nContent-Disposition: form-data; name=\"";
- data << file.first;
- data << "\"; filename=\"";
- data << inputFileName;
- data << "\"\r\nContent-Type: ";
- data << contentType;
- data << "\r\n\r\n";
- data << inputFileContent;
- }
- data << "\r\n--";
- data << boundary;
- data << "--\r\n";
- return { boundary, data.str() };
- }
|