Http Post Format for WCF Restful Service

前端 未结 4 1472
独厮守ぢ
独厮守ぢ 2020-12-30 15:26

Hey, super newbie question. Consider the following WCF function:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityReq         


        
4条回答
  •  轮回少年
    2020-12-30 16:29

    I've slightly changed your WCF service to have a better example and written a sample test program (see below).

    The first test executes a GET request for the URL http://localhost:57211/Service1.svc/getcar/1. The 1 at the end is a parameter. The port number may be different in your case. The result is:

    {"ID":1,"Make":"Porsche"}
    

    The second test executes a POST request by sending the same data (except Ferrari for Porsche) to the URL http://localhost:57211/Service1.svc/updatecar/1. The result is:

    {"ID":1,"Make":"Ferrari"}
    

    This request has both a parameter in the URL (the 1) plus the request data (a JSON structure) as a second parameter, transmitted as the request body.

    With a network debugger, it would look like this (simplified):

    POST /Service1.svc/updatecar/1 HTTP/1.1
    Host: localhost:57211
    Content-Type: application/json
    Content-Length: 25
    
    {"ID":1,"Make":"Ferrari"}
    
    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/10.0.0.0
    Date: Sat, 25 Dec 2010 19:16:19 GMT
    Content-Length: 25
    Content-Type: application/json; charset=utf-8
    
    {"ID":1,"Make":"Ferrari"}
    

    I hope that helps.

    TestService.cs:

    class TestService
    {
        static void Main(string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:57211/Service1.svc/getcar/1");
            WebResponse response = request.GetResponse();
            string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(result);
    
            string requestData = "{\"ID\":1,\"Make\":\"Ferrari\"}";
            byte[] data = Encoding.UTF8.GetBytes(requestData);
            request = (HttpWebRequest)WebRequest.Create("http://localhost:57211/Service1.svc/updatecar/1");
            request.Method = "POST";
            request.ContentType = "application/json";
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(data, 0, data.Length);
            dataStream.Close();
    
            response = request.GetResponse();
            result = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(result);
        }
    }
    

    IService.cs:

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "/getcar/{id}")]
        Car GetCar(string id);
    
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "/updatecar/{id}")]
        Car UpdateCar(string id, Car car);
    }
    
    
    [DataContract]
    public class Car
    {
        [DataMember]
        public int ID { get; set; }
    
        [DataMember]
        public string Make { get; set; }
    }
    

    Service.svc:

    public class Service1 : IService1
    {
        public Car GetCar(string id)
        {
            return new Car { ID = int.Parse(id), Make = "Porsche" };
        }
    
    
        public Car UpdateCar(string f, Car car)
        {
            return car;
        }
    }
    

    Service1.svc (Markup):

    <%@ ServiceHost Language="C#" Debug="true" Service="JSONService.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
    

提交回复
热议问题