• 物联网通信


    概述

    Server开放RESTful API接口,供应用程序/移动App/嵌入式qt通过http post调用,实现获取服务端数据,更新服务器数据

    详细

     

    一、前言

    什么是REST

    REST即表述性状态传递(英文:Representational State Transfer,简称REST),描述的是在网络中client和server的一种交互形式。

     

    REST能干什么

    REST可以通过一套统一的接口为 Web,iOS和Android提供服务。另外对于广大平台来说,比如Facebook platform,微博开放平台,微信公共平台等,它们不需要有显式的前端,只需要一套提供服务的接口,于是REST更是它们最好的选择。

    QQ图片20180831190240.png

     

    二、主要思路

    RestServer实现思路

    1. 搭建REST WCF服务

    2. 实现调用服务

    RestClient实现思路

    1. 使用C#编程语言访问RestServer提供的各个API接口,并得到返回值

    2. 使用JAVA编程语言访问RestServer提供的各个API接口,并得到返回值

    API接口说明:

    测试接口: http://127.0.0.1:8888/JsonService/Test

    参数接口: http://127.0.0.1:8888/JsonService/MultiParam

    获取数据(未加密)接口: http://127.0.0.1:8888/JsonService/GetDataTable

    获取数据(DES加密)接口: http://127.0.0.1:8888/JsonService/GetDataTable_DES

    执行操作(未加密)接口: http://127.0.0.1:8888/JsonService/ExecuteNonQuery

    执行操作(DES加密)接口: http://127.0.0.1:8888/JsonService/ExecuteNonQuery_DES

    三、效果演示

    服务端程序(C#)

    QQ图片20180831195605.jpg

    客户端程序(c#)

    客户端程序(java)

    QQ图片20180831195148.png

    四、代码框架

    QQ图片20180831200206.png

     

    五、程序实现

    RestServer实现

    1.配置文件app.config

    <?xml version="1.0"?>
    <configuration>
    <system.serviceModel>
    <services>
    <service name="ResetServer.JsonService">
    <endpoint address="http://127.0.0.1:8888/JsonService" binding="webHttpBinding" contract="ResetServer.IService" />
    </service>
    </services>
    </system.serviceModel>
    </configuration>

    添加接口

    [ServiceContract]
    public interface IService
    {
    // 测试接口
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "Test",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string Test();

    // 多个参数接口
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "MultiParam",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string MultiParam(string strParam1, string strParam2);

    // 查询Sql语句(未加密)
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "GetDataTable",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string GetDataTable(string strSql);

    // 查询Sql语句(DES加密)
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "GetDataTable_DES",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string GetDataTable_DES(string strSql);

    // 执行Sql语句(未加密)
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "ExecuteNonQuery",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string ExecuteNonQuery(string strSql);

    // 执行Sql语句(DES加密)
    [OperationContract]
    [WebInvoke(Method = "POST",
    UriTemplate = "ExecuteNonQuery_DES",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string ExecuteNonQuery_DES(string strSql);
    }

    实现接口

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class JsonService : IService
    {
    public string Test()
    {
    return JsonConvert.SerializeObject(new ResponseModel((int)ResponseEnum.Success, "ok", "", new DataTable()));
    }

    public string MultiParam(string strParam1, string strParam2)
    {
    Console.WriteLine("执行函数 [MultiParam]");
    return Response("strParam1=" + strParam1 + ",strParam2=" + strParam2, ResponseEnum.Success, "ok", "", new DataTable());
    }

    public string GetDataTable(string strSql)
    {
    Console.WriteLine("执行函数 [GetDataTable]");
    DataTable dt = QueryDB(strSql);
    return Response("strSql=" + strSql, ResponseEnum.Success, "ok", "", dt);
    }
    }

    RestClient实现

    PostResult HttpPost(string method, string param)
    {
    PostResult ret = new PostResult();

    try
    {
    // 返回结果
    // {"code":0,"info":"ok","msg":"","data":null}
    // {"code":0,"info":"ok","msg":"","data":[{"field1":"value10","field2":"value20"},{"field1":"value11","field2":"value21"}]}
    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    client.Headers.Add("Content-Type", "application/json");
    string jsonBack = client.UploadString(m_strUrl + method, "POST", param);
    jsonBack = jsonBack.Replace(@"", ""); // "{"code":0,"info":"ok","msg":"","data":null}"
    jsonBack = jsonBack.Substring(1, jsonBack.Length - 2);
    JObject jsonInfo = (JObject)JsonConvert.DeserializeObject(jsonBack);
    if (jsonBack.Contains("code"))
    {
    if (0 == jsonInfo.Value<int>("code")) ret.IsSuccess = true;
    ret.Info = jsonInfo.Value<string>("info");
    ret.ErrMsg = jsonInfo.Value<string>("errmsg");
    JArray arrayData = jsonInfo.Value<JArray>("data");
    if (null != arrayData) ret.Data = JsonConvert.DeserializeObject<DataTable>(arrayData.ToString());
    }
    }
    catch (Exception ex)
    {
    ret.ErrMsg = ex.Message;
    }

    return ret;
    }

    六、其他说明

    代码发布前已测试过,有什么疑问可以留言

    注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

  • 相关阅读:
    net start mongodb 提示:发生系统错误 5,拒绝访问。
    jquery下载所有版本
    国内优秀开源镜像站汇总
    bootstrap导航条报错 Uncaught TypeError: Cannot convert object to primitive value
    null的坑 和 比较运算符、相等运算符的隐式转换问题 (在javascript中,null>=0 为真,null<=0 为真,null==0却为假,null到底是什么?)
    关于 圣杯布局(双飞翼布局)的一些想法
    如何制作图标字体(如何将svg转换为css可用的图标字体)
    VirtualBox-虚拟硬盘扩容-win7
    前端JS导出表格
    JS判断是否是IE浏览器
  • 原文地址:https://www.cnblogs.com/demodashi/p/9582466.html
Copyright © 2020-2023  润新知