概述

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

详细

一、前言

什么是REST

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

REST能干什么

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

二、主要思路

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#)

客户端程序(c#)

客户端程序(java)

四、代码框架

五、程序实现

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大师发表,拒绝转载,转载需要作者授权

物联网通信 - RESTDemo示例程序的更多相关文章

  1. 物联网通信 - RESTDemo示例程序(C#版本)

    技术:wcf+http post+json(.net4.0 + jdk1.8) 运行环境:vs2010+java 概述Server开放RESTful API接口,供应用程序/移动App/嵌入式qt通过 ...

  2. 物联网通信 - RESTDemo示例程序(Python版本)

    QQ:505645074 下载地址: https://pan.baidu.com/s/1VHtni6rVslXkSBTW26jXTg GET接口 http://127.0.0.1:5000/test/ ...

  3. 物联网通信 - RESTDemo示例程序(Java版本)

    源码下载  -> 提取码  QQ:505645074 Netty的Restful API实现 Get: http://127.0.0.1:8662/test Post http://127.0. ...

  4. Windows Communication Foundation (WCF)和Windows CardSpace的示例程序

    微软公司昨天发布了一个Windows Communication Foundation (WCF)和Windows CardSpace的示例程序包,内容极为丰富,从最简单的Hello World到复杂 ...

  5. Socket编程指南及示例程序(转)

    1         前言 在一些常用的编程技术中,Socket网络编程可以说是最简单的一种.而且Socket编程需要的基础知识很少,适合初学者学习网络编程.目前支持网络传输的技术.语言和工具繁多,但是 ...

  6. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  7. .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序

    在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...

  8. .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序

    在之前的博文中我们在 Ubuntu 上成功编译出了 dnx ,并且用它成功运行了 ASP.NET 5 示例程序.在这篇博文中我们将 Ubuntu 换成 CentOS. 目前 dnx 的编译需要用到 m ...

  9. Salesforce Apex 使用JSON数据的示例程序

    本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...

随机推荐

  1. 线段树--codevs 1690 开关灯

    codevs 1690 开关灯 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description YYX家门前的街上有N(2& ...

  2. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. CodeM资格赛2

    题目描述 组委会正在为美团点评CodeM大赛的决赛设计新赛制. 比赛有 n 个人参加(其中 n 为2的幂),每个参赛者根据资格赛和预赛.复赛的成绩,会有不同的积分.比赛采取锦标赛赛制,分轮次进行,设某 ...

  4. Wide-range regulator delivers 12V, 3A output from 16 to 100V source

    Synchronous buck regulators offer high efficiency and are popular in applications in which available ...

  5. python接口自动化23-token参数关联登录(登录拉勾网)

    前言 登录网站的时候,经常会遇到传token参数,token关联并不难,难的是找出服务器第一次返回token的值所在的位置,取出来后就可以动态关联了 登录拉勾网 1.先找到登录首页https://pa ...

  6. [翻译] SFRoundProgressCounterView 带有进度显示的倒计时视图

    SFRoundProgressCounterView 带有进度显示的倒计时视图 https://github.com/simpliflow/SFRoundProgressCounterView A c ...

  7. iOS:面向对象的思想使用sqlite数据库

    SQLite支持的常见数据类型如下所示. –INTEGER 有符号的整数类型 –REAL 浮点类型 –TEXT 字符串类型,采用UTF-8和UTF-16字符编码 –BLOB 二进制大对象类型,能够存放 ...

  8. add-strings

    https://leetcode.com/problems/add-strings/ package com.company; import java.util.LinkedList; import ...

  9. 这篇讲PHP的讲的有些道理 & mb_substr & 中文处理

    http://chengxu.org/p/239.html Python 是否是下一个 PHP? 1. PHP胜在最要命的部署上:没有任何其他语言有像 PHP 一样适合大规模部署的方式.基本上装好 A ...

  10. 改善你的jQuery的25个步骤

    1. 从Google Code加载jQueryGoogle Code上已经托管了多种JavaScript类库,从Google Code上加载jQuery比直接从你的服务器加载更有优势.它节省了你服务器 ...