• c++使用http请求-drogon框架


    1. 创建drogon框架
        drogon_ctl create project test_ctrl
    
    • 1
    1. 添加一个控制器
    • 进入controllers目录下
        drogon_ctl create controller -h check_ctrl
    
    • 1
    1. 编写主函数
    
    #include 
    int main() {
        //Set HTTP listener address and port
        //drogon::app().addListener("0.0.0.0",80);
        //Load config file
    
    #ifdef NDEBUG
        drogon::app().loadConfigFile("./config.json");
    #else
        drogon::app().loadConfigFile("../../../config.json");
    #endif
        drogon::app().run();
        return 0;
    }
    
    配置文件初步介绍,进需要做如何配置
    // 这一部分是http监听的端口和ip地址
      "listeners": [
        {
          //address: Ip address,0.0.0.0 by default
          "address": "0.0.0.0",
          //port: Port number
          "port": 8080,
          //https: If true, use https for security,false by default
          "https": false
        }
        // ,
        // {
        //   "address": "0.0.0.0",
        //   "port": 443,
        //   "https": true,
        //   //cert,key: Cert file path and key file path, empty by default,0.0.1
        //   //if empty, use the global setting
        //   "cert": "",
        //   "key": "",
        //   //use_old_tls: enable the TLS1.0/1.1, false by default
        //   "use_old_tls": false,
        //   "ssl_conf": [
        //     //["MinProtocol", "TLSv1.3"]
        //   ]
        // }
      ],
    //------------------
    // 这一部分是在程序中的配置文件部分需要使用app().getCustomConfig(),由getCustomConfig得到的变量直接["path"]就可以不需要["custom_config"]["path"]
      //custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method.
      "custom_config": {
        "path": "D:/aaaa/bbb/",
        "time": 8,  // 小时:文件名+~小时得到文件的生成时间
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    1. 控制器.h文件
    #include 
    #include 
    
    using namespace drogon;
    using namespace std;
    
    class check_ctrl : public drogon::HttpController<check_ctrl>
    {
      public:
        METHOD_LIST_BEGIN
       	 METHOD_ADD(check_ctrl::test, "/test?", Post, Get);
        METHOD_LIST_END
      
       void test(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback);
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 控制器.cpp文件
    void check_ctrl::test(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback)
    {
    
        Json::Value ret;
        // 获取配置文件参数
        auto config = app().getCustomConfig();
        if (config.empty())
        {
            ret["success"] = false;
            ret["error_msg"] = "Empty custom config!";
            callback(HttpResponse::newHttpJsonResponse(ret));
            return;
        }
        // 读取配置文件参数
        std::string bz2_file = config["path"].asString();   // 文件地址
        int diff_time = config["time"].asInt();      // 文件时间需要小时 
    		
    		// ........
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    【C++】类和对象(下)
    Redis01——Redis简介
    非常有用的JavaScript高阶面试技巧!
    大规模新型冠状病毒疫情最优应对策略研究
    Go语言学习(四)-- 流程控制
    异步FIFO设计的仿真与综合技术(6)
    鸿蒙Next怎么升级,有便捷的方法?
    springcloudgateway Actuator API
    基于模态凝聚算法的特征系统实现算法的自然激励技术(Matlab代码实现)
    【Java基础】位运算符与原码,补码,反码
  • 原文地址:https://blog.csdn.net/mankeywang/article/details/132814473