Consume WCF Restful service POST method


Here I am Consuming WCF Restful service POST method to insert some data into database. Here I am sending the parameters as object to the service. I have created the webpage with some textbox controls to post some data to service.in button click I am accessing the service and saving data to database, find the code below:

“Client passed parameters must match with the service parameters” so just look at the article that shows how to create the servicewith POST method

ASPX Code:

<html>
<head runat="server">
    <title>fourthbottle</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Name : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        Addr : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        Mobi : <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
        Dept : <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" ForeColor="Green"></asp:Label>
    </div>
    </form>
</body>
</html>

Before writing aspx.cs Code add the below dll reference to the client solution as shown below


C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Net;
using System.Text;

namespace ConsumeService
{
    public class StudentDetails
    {
        public string StudentName { get; set; }
        public string SDepartment { get; set; }
        public string SAddress { get; set; }
        public string SMobile { get; set; }
    }

    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            StudentDetails stu = new StudentDetails
            {
                StudentName = TextBox1.Text,
                SAddress = TextBox2.Text,
                SMobile = TextBox3.Text,
                SDepartment = TextBox4.Text
            };
            DataContractJsonSerializer objseria = new DataContractJsonSerializer(typeof(StudentDetails));
            MemoryStream mem = new MemoryStream();
            objseria.WriteObject(mem, stu);
            string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
            WebClient webClient = new WebClient();
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadString("http://localhost:62013/Service1.svc/ADDStudent", "POST", data);
            Label1.Text = "Details saved using Rest service";
        }
    }
}