ASP.NET WebServce项目下添加Http服务,支持Get,Post请求方式;传输格式json/xml

由于WEBServce老项目中需要增添新的接口,而且添加的接口不希望被其它项目以引用Servces方式使用。

那么得在现有Service项目中添加Http请求方式来实现系统间数据交互。只需要告知请求地址,请求方法(GET,POST),和数据格式(JSON,XML)即可实现了。

客户端就如同调用普通HTTP接口一样。这样就不用强制引用啦!

 

要让WebService支持HTTP调用,首先需要在配置文件system.web节点下添加:

<webServices>

<protocols>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>

 

 

服务端代码实现:

首先添加文件 TestService.asmx,添加如下方法:

 1 ......
 2 
 3 [WebMethod]
 4 public void TestSer()
 5 {
 6 var paramsHt = GetFormPostData<ReqData>();
 7 
 8 Record record = new Record();
 9 
10 record.RetValue = new JavaScriptSerializer().Serialize(paramsHt);
11 List<Record> list = new List<Record>();
12 list.Add(record);
13 
14 ResponseJSON<Record>(list);
15 }
16 
17 
18 
19 //将请求数据转换为对象T
20 
21 public static T GetFormPostData<T>()
22 {
23 var reqData = HttpContext.Current.Request;
24 
25 var data = default(T);
26 using (StreamReader reqStream = new StreamReader(HttpContext.Current.Request.InputStream,System.Text.Encoding.UTF8))
27 {
28 data = new JavaScriptSerializer().Deserialize<T>(HttpContext.Current.Server.UrlDecode(reqStream.ReadToEnd()));
29 }
30 
31 return data;
32 }
33 
34 
35 
36 
37 public static void ResponseJSON<T>(List<T> ret)
38 {
39 string json = new JavaScriptSerializer().Serialize(ret);
40 
41 HttpResponse res = HttpContext.Current.Response;
42 res.ContentType = "text/plain";
43 res.Charset = "utf-8";
44 res.ContentEncoding = Encoding.UTF8;
45 
46 
47 res.Write(json);
48 
49 }
50 
51 
52 
53 //构造请求数据
54 
55 public class ReqData
56 {
57 public int ID { get; set; }
58 public string Name { get; set; }
59 }
60 
61 
62 
63 ......

 

 

WebForm请求端代码:

 1         protected void Button1_Click(object sender, EventArgs e)
 2         {
 3             string strURL = this.txtURL.Text.Trim();
 4 
 5             try
 6             {
 7                 byte[] postBytes = Encoding.UTF8.GetBytes(Server.UrlDecode(this.txtData.Text.Trim()));
 8                 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strURL);
 9                 myRequest.Method = "POST";
10                 myRequest.ContentType = "application/xml";
11                 //myRequest.ContentType = "text/html";
12                 myRequest.ContentLength = postBytes.Length;
13                 myRequest.Proxy = null;
14                 Stream newStream = myRequest.GetRequestStream();
15                 newStream.Write(postBytes, 0, postBytes.Length);
16                 newStream.Close();
17                 // Get response
18                 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
19                 using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
20                 {
21                     string content = reader.ReadToEnd();
22 
23                     this.lblResponseData.Text = content;
24                 }
25             }
26             catch (System.Exception ex)
27             {
28 
29                 this.lblResponseData.Text = ex.Message;
30             }
31  }

 

 

请求地址:http://localhost:43211/TestService.asmx/TestSer

请求数据:{"Name":"aa","ID":11}

 

 

当请求Json数据格式不规范时,可能会报错:传入的对象无效,应为“:”或“}”,请检查构造json数据是否规范,如双引号,分号,逗号,大括号等。

posted @ 2018-05-03 09:58  小站记忆  阅读(3383)  评论(0编辑  收藏  举报