Saturday, 11 May 2013

consuming rest service using webclient with sending parameter

var num1 = TextBox1.Text;
        var num2 = TextBox2.Text;
        string url = "http://localhost:4567/Service1.svc/JSONData/"+num1+"/"+num2;
        WebClient proxy = new WebClient();
        byte[] data = proxy.DownloadData( new Uri(url));
        Stream stream = new MemoryStream(data);
        DataContractSerializer
            obj = new DataContractSerializer(typeof(String));
        string result = obj.ReadObject(stream).ToString();
        Response.Write(result.ToString());


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService3
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {


        [OperationContract]
        [WebGet(UriTemplate = "/JSONData/{num1}/{num2}")]
        string JSONData(string num1,string num2);


        // TODO: Add your service operations here
    }
   
 
}

  public string JSONData(string num1,string num2)
        {
            int k = Convert.ToInt32(num1);
            int i = Convert.ToInt32(num2);
            int m = k + i;
            return m.ToString();
        }


<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
         <services>
                <service name="WcfService3.Service1" behaviorConfiguration="ServiceBehaviour">
                       <!-- Service Endpoints -->
                       <!-- Unless fully qualified, address is relative to base address supplied above -->
                       <endpoint address ="" binding="webHttpBinding" contract="WcfService3.IService1" behaviorConfiguration="web">
                             <!--
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          -->
                       </endpoint>
                       <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />


                </service>
         </services>
         <behaviors>
      <serviceBehaviors>
                <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
                <endpointBehaviors>
                       <behavior name="web">
                             <webHttp/>
                       </behavior>
                </endpointBehaviors>

         </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
 
</configuration>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Data;
using ServiceReference2;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ServiceClient obj = new ServiceClient();
        StringBuilder strbuilder = new StringBuilder();
        strbuilder.Append(@"{""User_Name"":""");
        strbuilder.Append(TextBox1.Text);
        strbuilder.Append(@"""}");
        string sendjson = strbuilder.ToString();
        Requestinput input = new Requestinput();
        input.key = "WEB";
        input.Memberdata = sendjson;
        List<Getemployee> list = new List<Getemployee>();
        Responseoutput res = obj.employee_info(input);
        if (res.Data != null)
        {
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(res.Data));
            DataContractJsonSerializer srlz = new DataContractJsonSerializer(typeof(Getemployee));
            Getemployee emp = (Getemployee)srlz.ReadObject(ms);
           
            list.Add(emp);
            //DataTable dt = new DataTable();
            //dt.Columns.Add("Department", typeof(string));
            //dt.Columns.Add("Post", typeof(string));
            //dt.Columns.Add("Project", typeof(string));
            //dt.Columns.Add("User_Name", typeof(string));
          
            //DataRow row = dt.NewRow();
            //row[0] = empdt.Department;
            //row[1] = empdt.Post;
            //row[2] = empdt.Project;
            //row[3] = empdt.User_Name;
            //dt.Rows.Add(row);

            GridView1.DataSource = list;
            GridView1.DataBind();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
    public Responseoutput employee_info(Requestinput input)
    {
        Responseoutput Output = new Responseoutput();
        jsonclsemployee_info _detail1 = new jsonclsemployee_info();
        if (input.key == "WEB")
        {
           
            getEmplyee emp = new getEmplyee();
            string json = input.Memberdata;
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
            DataContractJsonSerializer srlz = new DataContractJsonSerializer(emp.GetType());
            emp = (getEmplyee)srlz.ReadObject(ms);
            Clsemployee_info _details = new Clsemployee_info();
            if (emp.User_Name != null)
            {
                _details.User_Name = "mukesh";
                _details.Post = "Sw";
                _details.Project = "lmp";
                _details.Department = "IT";
               // _detail1.employeedetails = _details;
                MemoryStream M = new MemoryStream();
                DataContractJsonSerializer s = new DataContractJsonSerializer(_details.GetType());
                s.WriteObject(M, _details);
                UTF8Encoding encoding = new UTF8Encoding();
                string sendjson = encoding.GetString(M.ToArray());
                Output.Data = sendjson;
                Output.Message = "Yes";
            }
         
            }
        return Output;
        }
  
    }


<?xml version="1.0"?>
<configuration>

       <system.web>
              <compilation debug="true" targetFramework="4.0" />
       </system.web>
       <system.serviceModel>
              <services>
                     <service name="pocService1.Service" behaviorConfiguration="ServiceBehaviour">
                           <!-- Service Endpoints -->
                           <!-- Unless fully qualified, address is relative to base address supplied above -->
                           <endpoint address=""  binding="webHttpBinding" contract="pocService1.IService" behaviorConfiguration="web">
                           </endpoint>
                           <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />


                     </service>
              </services>
              <behaviors>
   <endpointBehaviors>
    <behavior name="web">
     <webHttp />
    </behavior>
   </endpointBehaviors>
   <serviceBehaviors>
    <behavior name="ServiceBehaviour">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    <behavior name="">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
              <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
       </system.serviceModel>
       <system.webServer>
              <modules runAllManagedModulesForAllRequests="true"/>
       </system.webServer>

</configuration>


No comments:

Post a Comment