RestFul WCF JSON Service with client and on Mozilla Firefox –REST Client
2015-10-24 14:57
651 查看
This article will talk about
WCF REStful service: with GET, PUT, POST, and DELETE methods
Web Invoke: Request response on JSON
See response on console in raw format
Testing on Mozilla Firefox – REST client
Automatic format selection for JSON/XML
parameters and return type. Like take string return object, take object return object, etc.
Copy Code
Web.config
Hide Copy Code
Copy Code
Copy Code
How to get and install: https://addons.mozilla.org/en-US/firefox/addon/restclient/
Testing:
本文属于个人转载,原文转载地址:http://www.codeproject.com/Articles/386956/RestFul-WCF-JSON-Service-with-client-and-on-Mozill
Introduction
This article will talk aboutWCF REStful service: with GET, PUT, POST, and DELETE methods
Web Invoke: Request response on JSON
See response on console in raw format
Testing on Mozilla Firefox – REST client
Automatic format selection for JSON/XML
Background
The RESTful web service is hosted on IIS. This service will take request and response on JSON. There is no business logic, this is just demo code. Let's see how to prepare a request for GET, PUT, POST, and DELETE methods. With a lot of combination of inputparameters and return type. Like take string return object, take object return object, etc.
Server side: Service
Hide ShrinkCopy Code
[ServiceContract] public interface IService1 { //GET - /// Simplet GET method with no parameret and return primitive type [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GetNoPara", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string HelloWorld(); /// GET Methos with parameter and return the Object [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GetDataJSON/{value}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] CompositeType GetDataJSON(string value); //POST /// Simple POST with primitive IN, OUT [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/PostString", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] bool PostTest(string value); /// POST Method whic accept Object as input and return Object [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/PostCompositeType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] CompositeType GetDataUsingDataContract(CompositeType composite); //PUT /// Simple PUT with primitive IN, OUT [OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "/PutString", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string PutString(string value); /// PUT Method whic accept Object as input and return string [OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "/PutCompositeType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string PutCompositeType(CompositeType composite); //DELETE /// DELETE Method whic accept string as input and return string [OperationContract] [WebInvoke(Method = "DELETE", UriTemplate = "/DeleteString", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string DeleteString(string value); /// DELETE Method whic accept object as input and return Object [OperationContract] [WebInvoke(Method = "DELETE", UriTemplate = "/DeleteCompositeType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] CompositeType DeleteCompositeType(string value);
Web.config
Setting for automatic format selection for JSON/XML – Just add the following onWeb.config
serviceBehaviors. This will automatically set the response format as per request type (JSON/XML):
Hide Copy Code
<endpointBehaviors> <behavior name="web"> <webHttp automaticFormatSelectionEnabled="true"/> </behavior> </endpointBehaviors>
Client side: WCF client
The C# Win application is going consume this service and we can see the response in raw data format for JSON and XML.Code
Hide ShrinkCopy Code
//================ GET======================== private void GetHello_Click(object sender, EventArgs e) { WebClient WC = new WebClient(); byte[] res1 = WC.DownloadData(ServiceUrl + "GetNoPara"); Stream res2 = new MemoryStream(res1); DataContractJsonSerializer res3 = new DataContractJsonSerializer(typeof(string)); string response = res3.ReadObject(res2).ToString(); Console.WriteLine(response); } private void HelloToMe_Click(object sender, EventArgs e) { GetData(ServiceUrl + "GetDataJSON/" + txtMyName.Text, "JSON"); WebClient WC = new WebClient(); byte[] res1 = WC.DownloadData(ServiceUrl + "GetDataJSON/" + txtMyName.Text); Stream res2 = new MemoryStream(res1); DataContractJsonSerializer res3 = new DataContractJsonSerializer(typeof(CompositeType)); CompositeType CT = (CompositeType)res3.ReadObject(res2); Console.WriteLine(CT.StringValue); } //=================== POST ========================== private void PassObject_Click(object sender, EventArgs e) { WebClient WC = new WebClient(); WC.Headers["Content-type"] = "application/json"; CompositeType CT = new CompositeType(); CT.BoolValue = true; CT.StringValue = txtMyName.Text; MemoryStream MS = new MemoryStream(); DataContractJsonSerializer JSrz = new DataContractJsonSerializer(typeof(CompositeType)); JSrz.WriteObject(MS, CT); byte[] res1 = WC.UploadData(ServiceUrl + "PostCompositeType", "POST", MS.ToArray()); Stream res2 = new MemoryStream(res1); JSrz = new DataContractJsonSerializer(typeof(CompositeType)); CT = (CompositeType)JSrz.ReadObject(res2); Console.WriteLine(CT.StringValue); } private void PostTest_Click(object sender, EventArgs e) { WebClient WC = new WebClient(); WC.Headers["Content-type"] = "application/json"; MemoryStream MS = new MemoryStream(); DataContractJsonSerializer JSrz = new DataContractJsonSerializer(typeof(string)); JSrz.WriteObject(MS, txtMyName.Text); byte[] res1 = WC.UploadData(ServiceUrl + "PostString", "POST", MS.ToArray()); MS = new MemoryStream(res1); JSrz = new DataContractJsonSerializer(typeof(bool)); bool result = (bool)JSrz.ReadObject(MS); Console.WriteLine(result); } #================== PUT ===================== private void PutString_Click(object sender, EventArgs e) { WebClient WC = new WebClient(); WC.Headers["Content-type"] = "application/json"; MemoryStream MS = new MemoryStream(); DataContractJsonSerializer JSrz = new DataContractJsonSerializer(typeof(string)); JSrz.WriteObject(MS, txtMyName.Text); byte[] res1 = WC.UploadData(ServiceUrl + "PutString", "PUT", MS.ToArray()); MS = new MemoryStream(res1); JSrz = new DataContractJsonSerializer(typeof(string)); string result = (string)JSrz.ReadObject(MS); Console.WriteLine(result); } private void PutCompositeType_Click(object sender, EventArgs e) { WebClient WC = new WebClient(); WC.Headers["Content-type"] = "application/json"; CompositeType CT = new CompositeType(); CT.BoolValue = true; CT.StringValue = txtMyName.Text; MemoryStream MS = new MemoryStream(); DataContractJsonSerializer JSrz = new DataContractJsonSerializer(typeof(CompositeType)); JSrz.WriteObject(MS, CT); byte[] res1 = WC.UploadData(ServiceUrl + "PutCompositeType", "PUT", MS.ToArray()); Stream res2 = new MemoryStream(res1); JSrz = new DataContractJsonSerializer(typeof(string)); string result = (string)JSrz.ReadObject(res2); Console.WriteLine(result); } #=================== DELETE ========================== private void DELETEString_Click(object sender, EventArgs e) { WebClient WC = new WebClient(); WC.Headers["Content-type"] = "application/json"; MemoryStream MS = new MemoryStream(); DataContractJsonSerializer JSrz = new DataContractJsonSerializer(typeof(string)); JSrz.WriteObject(MS, txtMyName.Text); byte[] res1 = WC.UploadData(ServiceUrl + "DeleteString", "DELETE", MS.ToArray()); MS = new MemoryStream(res1); JSrz = new DataContractJsonSerializer(typeof(string)); string result = (string)JSrz.ReadObject(MS); Console.WriteLine(result); } private void DELETECompositeType_Click(object sender, EventArgs e) { WebClient WC = new WebClient(); WC.Headers["Content-type"] = "application/json"; MemoryStream MS = new MemoryStream(); DataContractJsonSerializer JSrz = new DataContractJsonSerializer(typeof(string)); JSrz.WriteObject(MS, txtMyName.Text); byte[] res1 = WC.UploadData(ServiceUrl + "DeleteCompositeType", "DELETE", MS.ToArray()); MS = new MemoryStream(res1); JSrz = new DataContractJsonSerializer(typeof(CompositeType)); CompositeType CT = (CompositeType)JSrz.ReadObject(MS); Console.WriteLine(CT.StringValue); }
GET (same endpoint) automaticFormatSelectionEnabled
Hide ShrinkCopy Code
private void getDataJSON_Click(object sender, EventArgs e) { GetData("JSON"); } private void getDataXML_Click(object sender, EventArgs e) { GetData("XML"); } private void GetData(string ResponseType) { string URL = ServiceUrl + "AutoFormatPost/H"; GetData(URL, ResponseType); } private void GetData(string URL, string ResponseType) { WebClient client = new WebClient(); client.Headers["Content-type"] = @"application/" + ResponseType; Stream data = client.OpenRead(URL); StreamReader reader = new StreamReader(data); string str = ""; str = reader.ReadLine(); Console.WriteLine("******* Raw Response *******"); while (str != null) { Console.WriteLine(str); str = reader.ReadLine(); } data.Close(); }
Testing on Mozilla Firefox –REST Client
Mozilla Firefox will provide a tool to test a RESTful service and here we will see how to get that and use the same.How to get and install: https://addons.mozilla.org/en-US/firefox/addon/restclient/
Testing:
相关文章推荐
- JavaScript中Date对象的常用方法示例
- Angular.js学习笔记(三)
- js原生选择器的兼容性
- Json
- js&HTML 若干问题备忘
- JS读取用户输入的数据后,会自动存储为字符型
- javascript的继承模式
- alibaba的FastJson(高性能JSON开发包)
- 阅读笔记《JavaScript高级程序设计》
- js数据类型
- 前端Javascript书籍分享
- js常犯基本错误
- 对 Sea.js 进行配置 seajs.config
- js模块加载之AMD和CMD
- js获取项目根目录
- JavaScript 权威指南(4): JavaScript 的作用域和提升
- JSP中的编码问题
- jsp导出excel并支持分sheet导出的方法
- js中的this
- js中的this