. NET RESTful Web Services,. netrestful

Source: Internet
Author: User

. NET RESTful Web Services,. netrestful

I have seen RESTful Web Services a long time ago. I didn't care about it, nor did I find any relevant materials to learn. Today, I have an occasional opportunity to find some research materials and find that RESTful is "simple but not simple ". The following is an example:

1. Project Structure

2 REST Service Interface Definition

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. serviceModel; 6 using System. serviceModel. web; 7 namespace Jack_Restful_Service 8 {9 10 [ServiceContract (Name = "RestfulService", Namespace = "http://www.cnblogs.com/isaboy")] 11 public interface IRestDemoServices12 {13 [OperationContract] 14 [WebGet (UriTemplate = Routing. getClientRoute, BodyStyle = WebMessageBodyStyle. bare)] 15 string GetClientNameById (string Id); 16 17 [OperationContract] 18 [WebGet (UriTemplate = Routing. addClientRoute, BodyStyle = WebMessageBodyStyle. bare)] 19 string Add (string a, string B); 20 // error21 // string Add (int a, int B ); 22 23 [OperationContract] 24 [WebGet (UriTemplate = Routing. loginClientRoute, BodyStyle = WebMessageBodyStyle. bare)] 25 string Login (string uname, string upwd); 26 27 // post 28 [OperationContract] 29 [WebInvoke (RequestFormat = WebMessageFormat. json, 30 ResponseFormat = WebMessageFormat. json, 31 BodyStyle = WebMessageBodyStyle. bare, 32 Method = "POST", UriTemplate = "/Client/UpdateUser/{uname}")] 33 User UpdateUser (string uname, User newUser ); 34 35} 36 // URI Route 37 public static class Routing38 {39 public const string GetClientRoute = "/Client/{id }"; 40 41 public const string AddClientRoute = "/Client/{a}, {B}"; 42 // {uname} the parameter name and string Login (string uname, string upwd); 43 public const string LoginClientRoute = "/Client/{uname }__ {upwd}"; 44} 45 46 47}

 

3 REST service interface implementation

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ServiceModel; 6 using System.ServiceModel.Activation; 7 namespace Jack_Restful_Service 8 { 9 10     [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,11         ConcurrencyMode = ConcurrencyMode.Single,12         IncludeExceptionDetailInFaults = true,13         Namespace = "http://www.cnblogs.com/isaboy")]14     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]15     public class RestDemoServices : IRestDemoServices16     {17         //GET18         public string GetClientNameById(string Id)19         {20             string ReturnString = "Your id is: " + Id;21 22             return ReturnString;23         }24 25         public string Add(string a, string b)26         {27             int sum = int.Parse(a) + int.Parse(b);28             return sum.ToString();29         }30 31         public string Login(string uname, string upwd)32         {33             if (uname == "admin" && upwd == "admin")34             {35                 return "success";36             }37             else38             {39                 return "false";40             }41         }42         //POST43         public User UpdateUser(string uname, User newUser)44         {45             return newUser;46         }47     }48 49 }

 

4. HOST

1  Console.WriteLine("----------Restful Service Start--------------");2  RestDemoServices demoServices = new RestDemoServices();3  WebServiceHost _serviceHost = new WebServiceHost(demoServices, new Uri("http://localhost:8000/RestfulService"));4   _serviceHost.Open();5   Console.WriteLine("----------Restful Service Opened--------------");6   Console.WriteLine("http://localhost:8000/RestfulService/Client/8");7   Console.WriteLine("http://localhost:8000/RestfulService/Client/2,5");8   Console.WriteLine("http://localhost:8000/RestfulService/Client/admin__admin");

5. Open your browser to access resources.

In addition, we can use the code for testing.

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Net; 7 using System.IO; 8 namespace PostServiceTest 9 {10     class Program11     {12         static void Main(string[] args)13         {14             //get15             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8000/RestfulService/Client/8");16             WebResponse response = request.GetResponse();17             string result = new StreamReader(response.GetResponseStream()).ReadToEnd();18             Console.WriteLine(result);19 20             //post21             string requestData = "{\"uname\":\"admin\",\"upwd\":\"admin\"}";22             byte[] data = Encoding.UTF8.GetBytes(requestData);23             request = (HttpWebRequest)WebRequest.Create("http://localhost:8000/RestfulService/Client/UpdateUser/admin");24             request.Method = "POST";25             request.ContentType = "application/json";26             Stream dataStream = request.GetRequestStream();27             dataStream.Write(data, 0, data.Length);28             dataStream.Close();29 30             response = request.GetResponse();31             result = new StreamReader(response.GetResponseStream()).ReadToEnd();32             Console.WriteLine(result);33             Console.ReadKey();34         }35     }36 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.