Web service是一个基于可编程的web的应用程序,用于开发分布式的互操作的应用程序,也是一种web服务
WebService的特性有以下几点:
1.使用XML(标准通用标记语言)来作为数据交互的格式。
2.跨平台性,因为使用XML所以只要本地应用可以连接网络解析XML就可以实现数据交换,比如安卓、IOS、WindowsPhone等都可以实现对Web service的数据交互。
3.基于HTTP协议,直接跨越防火墙,通用型强;
界面中贴的代码只是主要实现流程,具体的实现方法,外部方法见项目资源源码下载
先根据这篇教程了解怎么简单创建一个web服务,以便更好深入了解
C#中WebService的创建和调用
Application_Start 和 Application_End
第一次访问站点时,创建HttpApplication对象,此时会触发Application_Start,并创建HttpApplication实例池,应用接请求就从池中获取实例,进行处理。在所有的HttpApplication实例闲置达到超时时间,触发应用程序池回收,或者重启站点时就会触发Application_End事件,应用程序池的闲置超时时间可以在iis设置,站点下bin目录下的文件发生改变,webconfig配置改变等导致站点重启的事件都会触发Application_End。
Session_Start 和 Session_End
单个用户访问Web应用时,启动会话,服务器为该用户创建一个独立的Session对象,并触发Session_Start事件,此时Session处于可用状态。用户在该会话建立后可以发起若干次请求,当服务器一段时间内未收到用户请求,达到会话超时时间时,触发Session_End事件,服务器释放为当前用户保存Session的内存。也可以在应用程序中调用Session.Abandon()可以手动取消Session,清空服务器保存的Session,直到再次调用Session时,又会触发Session_Start,但是SessionID不会变化。所有会触发Application_End的事件都会在此之前触发Session_Start。可以在Web.Config中添加设置Session过期时间(timeout单位为分钟)。
Application_BeginRequest 和 Application_EndRequest
在用户会话启动后,每次发起的请求都会触发Application_BeginRequest事件,并在请求完成时触发Application_EndRequest事件。
Application_BeginRequest 里配置过滤#region 过滤客户端xss恶意脚本提交
if (Request.COOKIEs != null)
{if (XSSFilter.COOKIEData()){Response.Charset = ConfigurationHelperUtil.DSA_Encode;Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Response.Write(JsonUtil.getText1Json("您提交的COOKIE数据有恶意字符!"));Response.End();}
}
if (Request.UrlReferrer != null)
{if (XSSFilter.referer()){Response.Charset = ConfigurationHelperUtil.DSA_Encode;Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Response.Write(JsonUtil.getText1Json("您提交的Referrer数据有恶意字符!"));Response.End();}
}
if (Request.RequestType.ToUpper() == "POST")
{if (XSSFilter.PostData()){Response.Charset = ConfigurationHelperUtil.DSA_Encode;Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Response.Write(JsonUtil.getText1Json("您提交的Post数据有恶意字符!"));Response.End();}
}
if (Request.RequestType.ToUpper() == "GET")
{if (XSSFilter.GetData()){Response.Charset = ConfigurationHelperUtil.DSA_Encode;Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Response.Write(JsonUtil.getText1Json("您提交的Get数据有恶意字符!"));Response.End();}
}
#endregion#region 过滤参数//遍历Post参数,隐藏域除外foreach (string i in this.Request.Form){if (i == "__VIEWSTATE") continue;this.goErr(this.Request.Form[i].ToString());}//遍历Get参数。 foreach (string i in this.Request.QueryString){this.goErr(this.Request.QueryString[i].ToString());}#endregion
Web.config
建好后的界面配置
dwsh.asmx代码
using DataServiceBLL;
using DataServiceUtil;
using System;
using System.Web.Script.Services;
using System.Web.Services;namespace DataServiceAPI.dsa.api.v1.data
{///
}
代码
#region 新增
[WebMethod(Description = "数据新增")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void add(string token, string name, string codedata, string detail)
{if (token != ConfigurationHelperUtil.DSA_TOKEN){Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(JsonUtil.getError205Json());//可json,可文本,可xmlContext.Response.End();}else{string result = "操作成功!";int code = 200;try{string sql = "";string db = ConfigurationHelperUtil.DSA_DB;int num = 0;if (db.ToUpper() == "MYSQL"){sql = "insert into SMS_TEST(NAME, CODE, DETAIL, INSERTTIME)";sql += " values('" + name + "', '" + codedata + "', '" + detail + "', now())";num = operDataBLL.OperData(sql);}else if (db.ToUpper() == "ORACLE"){sql = "insert into SMS_TEST(id,NAME,CODE,DETAIL,INSERTTIME)";sql += " values(seq_dswh_id.nextval, '" + name + "', '" + codedata + "', '" + detail + "', sysdate)";num = operDataBLL.OperOracleData(sql);}OperLogUtil.WriteFileLog("新增操作sql:" + sql, ConfigurationHelperUtil.DSA_LOG_TYPE_INFO);if (num > 0){result = "操作成功";}else{result = "未更新数据";code = 201;}}catch (Exception){result = "操作异常!";code = 202;}string resultJson = JsonUtil.getTextJson(code, result, 1, "[]");Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(resultJson);//可json,可文本,可xmlContext.Response.End();}
}
#endregion
代码
#region 修改
[WebMethod(Description = "数据修改")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void update(string token,string id, string name, string codedata, string detail)
{if (token != ConfigurationHelperUtil.DSA_TOKEN){Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(JsonUtil.getError205Json());//可json,可文本,可xmlContext.Response.End();}else{string result = "操作成功!";int code = 200;try{string sql = "";string db = ConfigurationHelperUtil.DSA_DB;int num = 0;if (db.ToUpper() == "MYSQL"){sql = "update SMS_TEST set NAME='" + name + "', CODE= '" + codedata + "', DETAIL='" + detail + "' where id="+id;num = operDataBLL.OperData(sql);}else if (db.ToUpper() == "ORACLE"){sql = "update SMS_TEST set NAME= '" + name + "',CODE= '" + codedata + "',DETAIL= '" + detail + "' where id="+id;num = operDataBLL.OperOracleData(sql);}OperLogUtil.WriteFileLog("修改操作sql:" + sql, ConfigurationHelperUtil.DSA_LOG_TYPE_INFO);if (num > 0){result = "操作成功";}else{result = "未更新数据";code = 201;}}catch (Exception){result = "操作异常!";code = 202;}string resultJson = JsonUtil.getTextJson(code, result, 1, "[]");Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(resultJson);//可json,可文本,可xmlContext.Response.End();}
}
#endregion
代码
#region 查询列表
[WebMethod(Description = "列表")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
//[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public void getlist(string token, string name)
{if (token != ConfigurationHelperUtil.DSA_TOKEN){Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(JsonUtil.getError205Json());//可json,可文本,可xmlContext.Response.End();}else{string result = "";try{if (name == "" || name == null || name == "null"){name = "";}else{name = " and name like '%" + name + "%'";}string sql = "select * from SMS_TEST t where 1=1" + name;string db = ConfigurationHelperUtil.DSA_DB;OperLogUtil.WriteFileLog("查询操作sql:" + sql, ConfigurationHelperUtil.DSA_LOG_TYPE_INFO);if (db.ToUpper() == "MYSQL"){result = operDataBLL.getData(sql);}else if (db.ToUpper() == "ORACLE"){result = operDataBLL.getOracleData(sql);}}catch (Exception){result = JsonUtil.getError201Json();}Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(result);//可json,可文本,可xmlContext.Response.End();}
}#endregion
代码
#region 删除
[WebMethod(Description = "数据删除")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void del(string token, string id)
{if (token != ConfigurationHelperUtil.DSA_TOKEN){Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(JsonUtil.getError205Json());//可json,可文本,可xmlContext.Response.End();}else{string result = "操作成功!";int code = 200;try{string sql = "";string db = ConfigurationHelperUtil.DSA_DB;int num = 0;if (db.ToUpper() == "MYSQL"){sql = "delete SMS_TESTdelete from SMS_TEST where id="+id;num = operDataBLL.OperData(sql);}else if (db.ToUpper() == "ORACLE"){sql = "delete SMS_TEST where id="+id;num = operDataBLL.OperOracleData(sql);}OperLogUtil.WriteFileLog("删除操作sql:" + sql, ConfigurationHelperUtil.DSA_LOG_TYPE_INFO);if (num > 0){result = "操作成功";}else{result = "未更新数据";code = 201;}}catch (Exception){result = "操作异常!";code = 202;}string resultJson = JsonUtil.getTextJson(code, result, 1, "[]");Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(resultJson);//可json,可文本,可xmlContext.Response.End();}
}
#endregion
代码
#region 查询列表分页
[WebMethod(Description = "分页列表")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
//[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public void getfyList(string token, string limit, string start, string name)
{if (token != ConfigurationHelperUtil.DSA_TOKEN){Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(JsonUtil.getError205Json());//可json,可文本,可xmlContext.Response.End();}else{string result = "";if (!StrUtil.isNum(limit) || !StrUtil.isNum(start))//验证是否合格参数{result = JsonUtil.getError203Json();}else{try{if (name == "" || name == null || name == "null"){name = "";}else{name = " and name like '%" + name + "%'";}string db = ConfigurationHelperUtil.DSA_DB;if (db.ToUpper() == "MYSQL"){//limit a,b a为开始行数 b为几行int ks = (int.Parse(start) - 1) * int.Parse(limit);string sql = "SELECT * FROM SMS_TEST WHERE 1=1" + name + " ORDER BY INSERTTIME desc LIMIT " + ks + ", " + limit;result = operDataBLL.getFyData(sql, "SMS_TEST");OperLogUtil.WriteFileLog("分页列表操作sql:" + sql, ConfigurationHelperUtil.DSA_LOG_TYPE_INFO);}else if (db.ToUpper() == "ORACLE"){string sql = "SELECT * FROM SMS_TEST WHERE 1=1" + name;//false desc true ascresult = operDataBLL.getFyOracleData(sql, "INSERTTIME", false, int.Parse(limit), int.Parse(start));OperLogUtil.WriteFileLog("分页列表操作sql:" + sql, ConfigurationHelperUtil.DSA_LOG_TYPE_INFO);}}catch (Exception){result = JsonUtil.getError201Json();}}Context.Response.Charset = ConfigurationHelperUtil.DSA_Encode;Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ConfigurationHelperUtil.DSA_Encode);Context.Response.Write(result);//可json,可文本,可xmlContext.Response.End();}
}#endregion
protected void Application_Start(object sender, EventArgs e)
{//启动webScoket//Thread thread2 = new Thread(new ThreadStart(WebScoket.startWebScoket));//创建线程//thread2.Start(); //启动线程//WebScoket.startWebScoket();//用于CMS}
using DataServiceBLL;
using DataServiceUtil;
using Newtonsoft.Json.Linq;
using StriveEngine;
using StriveEngine.Core;
using StriveEngine.Tcp.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace DataServiceAPI.App_Code
{public class WebScoket{private static ITcpServerEngine tcpServerEngine;private static bool hasTcpServerEngineInitialized;public static OperDataBLL operDataBLL = new OperDataBLL();public static void startWebScoket(){OperLogUtil.WriteFileLog("启动WebScoket", ConfigurationHelperUtil.DSA_LOG_TYPE_WEBSCOKET);try{if (tcpServerEngine == null){tcpServerEngine = NetworkEngineFactory.CreateTextTcpServerEngine(int.Parse(ConfigurationHelperUtil.DSA_WebScoket), new DefaultTextContractHelper("\0"));//DefaultTextContractHelper是StriveEngine内置的ITextContractHelper实现。使用UTF-8对EndToken进行编码。 }if (hasTcpServerEngineInitialized){tcpServerEngine.ChangeListenerState(true);}else{InitializeTcpServerEngine();}}catch (Exception ee){OperLogUtil.WriteFileLog(ee.Message, ConfigurationHelperUtil.DSA_LOG_TYPE_ERROR);}}public static void InitializeTcpServerEngine(){tcpServerEngine.ClientCountChanged += new CbDelegate
【博主推荐】asp.net WebService 后台数据API JSON(附源码)