ASP.NET WebServce項目下添加Http服務,支持Get,Post請求方式;傳輸格式json/xml

因爲WEBServce老項目中須要增添新的接口,並且添加的接口不但願被其它項目以引用Servces方式使用。html

那麼得在現有Service項目中添加Http請求方式來實現系統間數據交互。只須要告知請求地址,請求方法(GET,POST),和數據格式(JSON,XML)便可實現了。web

客戶端就如同調用普通HTTP接口同樣。這樣就不用強制引用啦!json

 

要讓WebService支持HTTP調用,首先須要在配置文件system.web節點下添加:app

<webServices>post

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

 

 

服務端代碼實現:spa

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

 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請求端代碼:orm

 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/TestSerxml

請求數據:{"Name":"aa","ID":11}

 

 

當請求Json數據格式不規範時,可能會報錯:傳入的對象無效,應爲「:」或「}」,請檢查構造json數據是否規範,如雙引號,分號,逗號,大括號等。

相關文章
相關標籤/搜索