webservice示例
webservice示例:
webservice是什么:
WebService是一种跨编程语言和跨操作系统平台的远程调用技术。
所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。
using H3.BizBus; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Text; using System.Web; using System.Web.Services; namespace WebApplication4 { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string GetSchema(string schemaCode) { string strSchemaJson = string.Empty; BizStructureSchema schema = new BizStructureSchema();//创建一个业务结构 string errorMessage = string.Empty; schema.Code = schemaCode; schema.Add(new ItemSchema("Status", "返回结果", BizDataType.String)); schema.Add(new ItemSchema("Msg", "返回结果", BizDataType.String)); schema.Add(new ItemSchema("bbh", "bbh", BizDataType.String)); schema.Add(new ItemSchema("bzmc", "bzmc", BizDataType.String)); schema.Add(new ItemSchema("czbzId", "czbzId", BizDataType.Int)); schema.Add(new ItemSchema("id", "id", BizDataType.String)); schema.Add(new ItemSchema("jcdId", "jcdId", BizDataType.String)); schema.Add(new ItemSchema("jcdwtid", "jcdwtid", BizDataType.String)); schema.Add(new ItemSchema("bzlyId", "bzlyId", BizDataType.String)); schema.Add(new ItemSchema("xgsj", "xgsj", BizDataType.DateTime)); schema.Add(new ItemSchema("xssx", "xssx", BizDataType.Int)); schema.Add(new ItemSchema("zt", "zt", BizDataType.Int)); strSchemaJson = BizStructureUtility.SchemaToJson(schema); //string jsStr = Newtonsoft.Json.JsonConvert.SerializeObject(schemaCode); return strSchemaJson;//返回JSON字符串 } [WebMethod] public string GetSchemaList() { return string.Empty;//返回JSON字符串 } //获取查询页面id和数目条数 /// public string FJLdata(string json) { JObject obj = JObject.Parse(json); JArray jsonstr = (JArray)obj["Matcher"]["Matchers"]; string value = jsonstr[0]["Value"].ToString(); return value; } [WebMethod] public string GetList(string userCode, string schemaCode, string filter) { //调用成功 string intrr = "a. 有程序规定:< br >(1) 应列出对于确保符合经局方批准的设计至关重要的全部产品特性,对于不能保持就会导致或可能导致完工品出现不安全情况的关键特性应单独列出;< br >(2) 对完工品的关键特性,应进行100%检验;< br >(3) 选定的样本应能充分代表该批次或该工艺方法;< br >(4) 基于验收的质量履历来调整抽样计划,若发现影响安全的不合格品,则应加严为100%检验;< br >(5) 统计检验应符合抽样规范或批准的抽样计划要求;< br >(6) 抽样计划不允许接收带有影响安全的“已知缺陷”的批次、或带有影响安全的“已知缺陷”的可接受质量等级(AQL)。"; if (schemaCode == "D001728jianchabiaozhun") { #region //get方法不带参数进行调用 string url = "http://zxf.hyjktest.top/ruoyi-admin/Jc/data/GetJcInspect?currentPage=10&pageSize=10"; Encoding encodin = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "text/html,application/xhtml+xml,*/*"; request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); //string thejic = reader.ReadToEnd(); //JObject obj = JObject.Parse(thejic); //JArray jsonstr = (JArray)obj["data"]["list"]; //String hingeString = Newtonsoft.Json.JsonConvert.SerializeObject(jsonstr); //return hingeString;//返回JSON字符串 #endregion int dataCount = 0; //返回数据条数 int resultCodein = 0; //全局返回状态 0为成功 否则失败 string resultMsg = "全局返回信息"; //全局返回信息 H3.BizBus.BizStructureSchema schema = null; string errorMessage = string.Empty; //获取BizStructureSchema字段结构 BizStructureUtility.JsonToSchema(this.GetSchema(schemaCode), out schema, out errorMessage); //定义返回的数据集合,这里以返回多条数据为例 List<BizStructure> list = new List<BizStructure>(); //获取数据(第三方接口,第三方数据库等等,这里以DataTable为例) string thejic = reader.ReadToEnd(); JObject obj = JObject.Parse(thejic); JArray jsonstr = (JArray)obj["data"]["list"]; if (jsonstr != null && jsonstr.Count > 0) { for (int i = 0; i < jsonstr.Count; i++) { string jsonzhuanyi= Newtonsoft.Json.JsonConvert.SerializeObject(jsonstr[i]); bool czbzcunzai = jsonzhuanyi.Contains("czbzId"); bool xssxcunzai = jsonzhuanyi.Contains("xssx"); BizStructure bizStructure = new H3.BizBus.BizStructure(schema); bizStructure["Status"] = "true"; bizStructure["Msg"] = "msg"; bizStructure["bbh"] = jsonstr[i]["bbh"]; bizStructure["bzmc"] = jsonstr[i]["bzmc"]; //string abc = jsonstr[i]["czbzId"].ToString(); if (czbzcunzai == false) { bizStructure["czbzId"] = (int)12135; } else { bizStructure["czbzId"] = int.Parse(jsonstr[i]["czbzId"].ToString());} bizStructure["id"] = jsonstr[i]["id"]; bizStructure["jcdId"] = jsonstr[i]["jcdId"]; bizStructure["jcdwtid"] = jsonstr[i]["jcdwtid"]; bizStructure["bzlyId"] = jsonstr[i]["lybzid"]; bizStructure["xgsj"] = DateTime.Parse(jsonstr[i]["xgsj"].ToString()); if (xssxcunzai==false) { bizStructure["xssx"] = (int)12135; } else { bizStructure["xssx"] = int.Parse(jsonstr[i]["xssx"].ToString()); } bizStructure["zt"] = int.Parse(jsonstr[i]["zt"].ToString()); list.Add(bizStructure); } dataCount = list.Count; } ListResult listResult = new ListResult(resultCodein, resultMsg, list.ToArray(), dataCount); return BizStructureUtility.ListResultToJson(listResult); } else { return string.Empty; } } [WebMethod] //氚云后端调用的方法名 public string Invoke(string userCode, string schemaCode, string methodName, string param) { //书写调用第三方接口方法 return string.Empty;//返回JSON字符串 } } }
调用接口
string url = "http://zxf.hyjktest.top/ruoyi-admin/Jc/data/GetJcInspect?currentPage=10&pageSize=10"; Encoding encodin = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "text/html,application/xhtml+xml,*/*"; request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);