标签:catch 前端 编程语言 list htm ebe filter try 1.0
REST即表述性状态传递(英文:Representational State Transfer,简称REST),描述的是在网络中client和server的一种交互形式。
REST可以通过一套统一的接口为 Web,iOS和Android提供服务。另外对于广大平台来说,比如Facebook platform,微博开放平台,微信公共平台等,它们不需要有显式的前端,只需要一套提供服务的接口,于是REST更是它们最好的选择。
RestServer实现思路
搭建REST WCF服务
实现调用服务
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;
}
代码发布前已测试过,有什么疑问可以留言
标签:catch 前端 编程语言 list htm ebe filter try 1.0
原文地址:https://www.cnblogs.com/demodashi/p/9582466.html