How to pass parameter(s) to a WCF post method (a restful services)

前端 未结 1 1517
一向
一向 2021-01-07 04:17

I am working on a WCF rest based services. I have written Get and Post methods in my service and Get methods are able to working (fetching data) when I typed in URL (in JSON

相关标签:
1条回答
  • 2021-01-07 05:09

    Finally after many trials and many things, I got the working solution. Here I am posting what I did for the POST method to work.

      [OperationContract]
      [WebInvoke(Method = "POST", UriTemplate = "AddOrders", RequestFormat =   
      WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle = 
      MessageBodyStyle.Bare)]
      int AddOrders(RequestData orderRequestData);
    

    This is for implementation in the operation contract.

    Client application:-

            WebClient WC = new WebClient();
            WC.Headers.Add("Content-Type", "application/json");
            WC.Encoding = Encoding.UTF8;
    
            MemoryStream MS = new MemoryStream();
            DataContractJsonSerializer JSrz = new 
            DataContractJsonSerializer(typeof(RequestData));
            JSrz.WriteObject(MS, order);
            string data = Encoding.UTF8.GetString(MS.ToArray(), 0, (int)MS.Length);
    
            byte[] res1 = 
            WC.UploadData("http://localhost/EMCService/Service2.svc/AddOrders", "POST",MS.ToArray());
    
            MS = new MemoryStream(res1);
            JSrz = new DataContractJsonSerializer(typeof(int));
            int result = (int)JSrz.ReadObject(MS);
    

    I did not made any config settings and still using the old settings of web.config what I have posted in the above question, and it is working.

    0 讨论(0)
提交回复
热议问题