开发者中心 开发者中心
  • 简体中文
  • English
视频教程
敢为云网站
  • 6.0版本
  • 6.1 版本
视频教程
敢为云网站
  • 平台概述
  • 平台功能
  • 平台安装
  • 开发者指南
  • 项目实战
  • 附录
    • 常用协议说明
    • 常用调试工具
    • 接口说明
    • 数据库说明
    • 平台接入能力
    • 如何使用gRPC
    • 敢为公共库
      • 日志组件
      • 数据库组件
      • TCP公共库
      • Http公共库
        • 依赖库
        • 安装教程
        • 使用说明
          • 1 统一返回值
          • 2 在IoTCenter中使用统一http服务
    • 内网平台获取外网平台数据
    • 扩展插件使用说明
    • VForm3开发者文档
    • 虚拟计算表达式
    • 基于K8s负载均衡云服务配置
    • Q&A

http公共库

# 敢为公共库

通用http服务端组件,支持GET,POST请求方式 支持http模式,暂未支持https安全认证

# 依赖库

序号 名称 版本 说明
1 Microsoft.AspNetCore 2.2.0 AspNetCore核心库
2 Microsoft.AspNetCore.Mvc 2.2.0 AspNetCore mvc支持
3 Newtonsoft.Json 12.0.3 Json序列化和反序列化工具
4 IoTCenter.Extensions.Logging 1.1.0 日志组件

# 安装教程

通过Nuget(https://nuget.ganweicloud.com)安装

Install-Package IoTCenter.Extensions.WebApi

# 使用说明

int port = 12345;
HttpHost server = new HttpHost(port); //监听地址 http:/localhost:12345;
server.Start();//开启监听服务
server.Get["/"] = (handler) => "hello"; //注册路由
server.Post["/hello"] = (handler) => "hello";//注册路由
server.Register("/hello2", HttpMethod.Get, (handler) => "hello2");//注册路由

回调方法定义: public delegate object HttpRequestHandler(Microsoft.AspNetCore.Http.HttpRequest request);

# 1 统一返回值

所有返回值会使用JsonResult进行统一格式封装,Data对象为返回值内容

public class JsonResult
{
    public int Code { get; set; }
    public string Message { get; set; }
    public object Data { get; set; }
    
    public static JsonResult Result200(object data, string msg = "")
    {
        return new JsonResult
        {
            Code = 200,
            Message = msg,
            Data = data
        };
    }
    public static JsonResult Result404(string msg)
    {
        return new JsonResult
        {
            Code = 404,
            Message = msg
        };
    }
    public static JsonResult Result500(string msg)
    {
        return new JsonResult
        {
            Code = 500,
            Message = msg
        };
    }
}

# 2 在IoTCenter中使用统一http服务

根据项目需求,通常需要在服务不同模块中开通多个不同http服务以满足不同厂家或应用需求,常常会因为不同开发人员使用不同实现方式,导致功能或模块之间冲突; 使用IoTCenter.Extensions.WebApi创建统一的http服务,对接口进行统计管理,减少冲突问题及使用资源

# 2.1 HttpServer.STD 使用

# 2.2 GWExProc表配置

Proc_Code Proc_Module Proc_name Proc_parm
1(序号) GWHttpServer.STD.dll 公共Http服务 12345(监听端口)

# 2.3 注册路由

在模块中引用GWHttpServer.STD.dll文件 使用静态方法CExProc.Get 或 CExProc.Post 或 CExProc.Register(string route, HttpMethod method, HttpRequestHandler handler) 注册路由回调

CExProc.Get["/"] = (handler) => "hello"; //注册路由
CExProc.Post["/hello"] = (handler) => "hello";//注册路由
CExProc.Register("/hello2", HttpMethod.Get, (handler) => "hello2");//注册路由
上次更新: 2023/7/21 09:43:31

← TCP公共库 内网平台获取外网平台数据→

目录
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式