首页 > 编程语言 >C++ REST SDK

C++ REST SDK

时间:2023-01-17 10:35:24浏览次数:42  
标签:std web http string request C++ headers REST SDK


#include <iostream>
#include <future>
#include <string>
#include <sstream>
#include <stdexcept>
#include <functional>
#include <locale>
#include <codecvt>
//
#include "pplx/pplxtasks.h" // #include <ppltasks.h>
#include "cpprest/uri.h"
#include "cpprest/uri_builder.h"
#include "cpprest/http_client.h"
#include "cpprest/json.h"

-----------

//--------------------------------------------------------------------------------------------
static std::map<std::string, std::string> &headers2map(web::http::http_headers &headers)
{
  std::map<std::string, std::string> map;
  for (auto &header : headers)
  {
    // utility::conversions::to_string_t(header.first);
    map.emplace(utility::conversions::to_utf8string(header.first), utility::conversions::to_utf8string(header.second));
  }
  return std::move(map);
}

-----------

//--------------------------------------------------------------------------------------------
static void requestDirectSDK()
{ // https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120)

  std::string method = "GET"s;
  std::string url = "https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120)"s;
  std::map<std::string, std::string> headers{

  };
  //
  web::http::method methodPara = utility::conversions::to_string_t(method);
  //
  web::uri uri(utility::conversions::to_string_t(url));
  web::http::client::http_client client(uri);

  web::http::http_request request;
  request.set_method(methodPara);
  request.set_request_uri(L"");
  web::http::http_headers &req_headers = request.headers();
  // request.headers().add(web::http::header_names::accept, L"application/json");
  for (auto &item : headers)
  {
    req_headers.add(utility::conversions::to_string_t(item.first), utility::conversions::to_string_t(item.second));
  }
  if (methodPara != web::http::methods::GET && methodPara != web::http::methods::HEAD)
  {
    // if (data != "")
    // {
    //   // web::json::value jvalue = web::json::value::string(utility::conversions::to_string_t(data));
    //   // request.set_body(jvalue);    //responseTask = client.request(methodPara, L"", jvalue);
    //   request.set_body(utility::conversions::to_string_t(data));
    // }
  }
  // pplx::task<web::http::http_response> responseTask = client.request(request); // client.request(methodPara);
  pplx::task<void> &HTTPStreamingAsync = client.request(request).then([=](web::http::http_response &response)
                                                                      {
                                                                        web::http::status_code status = response.status_code();
                                                                        std::cout << "status:  " << status << std::endl;
                                                                        if (status != web::http::status_codes::OK)
                                                                        {
                                                                          return pplx::task_from_result();
                                                                        }
                                                                        std::map<std::string, std::string> &headers = headers2map(response.headers());
                                                                        std::string browserResponseType = utility::conversions::to_utf8string(response.headers().content_type());
                                                                        //
                                                                        std::wstring &result = response.extract_string(true).get();
                                                                        std::wcout << "size: " << result.size() << std::endl;
                                                                        std::wcout << result.substr(0, 500) << std::endl;
                                                                        return pplx::task_from_result(); });
  //
  // HTTPStreamingAsync.wait();
}
//--------------------------------------------------------------------------------------------

-----------

//--------------------------------------------------------------------------------------------
static void requestDirectSDKAsync()
{ // https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120)

  std::string method = "GET"s;
  std::string url = "https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120)"s;
  std::map<std::string, std::string> headers{

  };
  //
  web::http::method methodPara = utility::conversions::to_string_t(method);
  //
  web::uri uri(utility::conversions::to_string_t(url));
  web::http::client::http_client client(uri);

  web::http::http_request request;
  request.set_method(methodPara);
  request.set_request_uri(L"");
  web::http::http_headers &req_headers = request.headers();
  // request.headers().add(web::http::header_names::accept, L"application/json");
  for (auto &item : headers)
  {
    req_headers.add(utility::conversions::to_string_t(item.first), utility::conversions::to_string_t(item.second));
  }
  if (methodPara != web::http::methods::GET && methodPara != web::http::methods::HEAD)
  {
    // if (data != "")
    // {
    //   // web::json::value jvalue = web::json::value::string(utility::conversions::to_string_t(data));
    //   // request.set_body(jvalue);    //responseTask = client.request(methodPara, L"", jvalue);
    //   request.set_body(utility::conversions::to_string_t(data));
    // }
  }
  // pplx::task<web::http::http_response> responseTask = client.request(request); // client.request(methodPara);
  pplx::task<void> &HTTPStreamingAsync = client.request(request).then([=](web::http::http_response &response)
                                                                      {
                                                                        web::http::status_code status = response.status_code();
                                                                        std::cout << "status:  " << status << std::endl;
                                                                        if (status != web::http::status_codes::OK)
                                                                        {
                                                                          return pplx::task_from_result();
                                                                        }
                                                                        std::map<std::string, std::string> &headers = headers2map(response.headers());
                                                                        std::string browserResponseType = utility::conversions::to_utf8string(response.headers().content_type());
                                                                        // 返回string
                                                                        // return response.extract_string(true).then([](const std::wstring &result)
                                                                        //                                           {
                                                                        //                                             std::wcout << "size: " << result.size() << std::endl;
                                                                        //                                             std::wcout << result.substr(0, 500) << std::endl;
                                                                        //                                             //
                                                                        //                                           });
                                                                        // 返回 stream
                                                                        concurrency::streams::istream bodyStream = response.body();
                                                                        concurrency::streams::container_buffer<std::string> inStringBuffer;
                                                                        // we print the first 500 characters of the response to the console.
                                                                        // return bodyStream.read(inStringBuffer, 500).then([inStringBuffer](size_t bytesRead)
                                                                        //                                                 {
                                                                        //         const std::string &text = inStringBuffer.collection();
                                                                        //         // For demonstration, convert the response text to a wide-character string.
                                                                        //         std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv;
                                                                        //         std::wostringstream ss;
                                                                        //         ss << utf16conv.from_bytes(text.c_str());
                                                                        //         std::wcout << ss.str()<<std::endl; });
                                                                        // Read a line from the stream into a string.
                                                                        // return bodyStream.read_line(inStringBuffer).then([inStringBuffer](size_t bytesRead)
                                                                        //                                                  {
                                                                        //                                                    const std::string &line = inStringBuffer.collection();
                                                                        //                                                    std::cout << line << std::endl;
                                                                        //                                                    // Perform actions here after reading line into a string...
                                                                        //                                                  });
                                                                        // Read all string
                                                                        return bodyStream.read_to_end(inStringBuffer).then([inStringBuffer](size_t bytesRead)
                                                                                                                           {
                                                                                                                             const std::string &text = inStringBuffer.collection();
                                                                                                                             std::cout << text << std::endl;
                                                                                                                             //  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv;
                                                                                                                             //  std::wstring& str = utf16conv.from_bytes(text.c_str());
                                                                                                                             //  std::wostringstream wss;
                                                                                                                             //  wss << str<< std::endl;
                                                                                                                             //  std::wcout << wss.str() << std::endl;
                                                                                                                             //
                                                                                                                           });
                                                                        //
                                                                      });

  //
  // HTTPStreamingAsync.wait();
}
//--------------------------------------------------------------------------------------------

------------------------------

void PromiseCommand::OnClick()
{
  //
  requestDirectSDKAsync();
  // // HTTPStreamingAsync.wait();
  
}

标签:std,web,http,string,request,C++,headers,REST,SDK
From: https://www.cnblogs.com/gispathfinder/p/17057170.html

相关文章

  • C++ 树进阶系列之嘿!别绕了,这个问题可以使用并查集
    1.前言并查集是一种抽象数据结构,可以从名字上解释其功能。集:操作对象是集合群,并查集是与集合操作有关的数据结构。查:查询元素所在的集合。并:合并元素之间关系的集合。......
  • 【机器学习】集成学习(Bagging)——随机森林(RandomForest)(理论+图解+公式推导)
    如果需要完整代码可以关注下方公众号,后台回复“代码”即可获取,阿光期待着您的光临~文章目录​​一、引言​​​​二、随机森林​​​​1.数据抽样​​​​2.集成模型结果​......
  • c++ UTF8-GBK互转
    inlinestd::stringutf8_to_gbk(conststd::string&str){std::wstring_convert<std::codecvt_utf8<wchar_t>>conv;std::wstringtmp_wstr=conv.from_byte......
  • C++中的时间与时钟
    1.DurationTypes2.Clocks3.TimepointTypes4.CalendricalTypes5.TimeTypehh_mm_ss6.HoursUtilities......
  • C++代码与AST compiler
    C++代码与ASTcompilerCompiler3_语法制导翻译&AST语法制导翻译(SyntaxDirectedTranslation)的任务解析输入的字符串时,在特定位置执行指定的动作。基本思想   ......
  • C++11 智能指针 shared_ptr
    C++11智能指针shared_ptrWrittenon2023-01-16std::shared_ptr<T>共享智能指针,也被称为计数智能指针。共享智能指针会记录有多少个共享智能指针指向同一个对象,当......
  • C/C++ 顺序表的初始化、添加、插入、删除(删除顺序表中指定的所有元素)
    #include<iostream>#include<stdlib.h>#defineMAX_SIZE100usingnamespacestd;typedefstruct_SqList{int*elems;//顺序表的基地址intsize;//......
  • C++11 智能指针
    C++11智能指针Writtenon2023-01-16学习参考资料:C++现代实用教程:智能指针30分钟讲明白现代C++最重要的特性之一:智能指针动态内存管理官方文档栈对象、静态对......
  • C++文学研究助手[2023-01-16]
    C++文学研究助手[2023-01-16]综合实验18文学研究助手一、实验目的(1)熟练掌握串的基本操作及应用。(2)熟练掌握串的匹配操作算法。(3)基于串的存储和操作,实现对英文......
  • cita-sdk react16.9 依赖安装及运行问题经验记录
    运行环境查找选择node稳定版本发布时间,技术框架发布时间一致即可nodev10.18.0reactv16.9.0pythonv2.7.18安装cita-sdk一直报错上面两个错误一直循环报错,但最后......