I have written a webservice which on browser launch works fine. I pass a client id in this webservice and then returns a string containing the client name and it which we passed like this: http://prntscr.com/8c1g9z
My code for creating service is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace RESTService.Lib
{
[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
[OperationContract]
[WebGet(UriTemplate = "/Client/{id}", BodyStyle = WebMessageBodyStyle.Bare)]
string GetClientNameById(string Id);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDemoServices:IRESTDemoServices
{
public string GetClientNameById(string Id)
{
return ("Le nom de client est Jack et id est : " +Id);
}
}
}
But I am not able to consume it. My code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Net;
using System.IO;
namespace ConsumerClient
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Net.HttpWebRequest webrequest = (HttpWebRequest)System.Net.WebRequest.Create("http://localhost:8000/DEMOService/Client/156");
webrequest.Method = "POST";
webrequest.ContentType = "application/json";
webrequest.ContentLength = 0;
Stream stream = webrequest.GetRequestStream();
stream.Close();
string result;
using (WebResponse response = webrequest.GetResponse()) //It gives exception at this line like this http://prntscr.com/8c1gye
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
Label1.Text = Convert.ToString(result);
}
}
}
}
}
I get an exception like this http://prntscr.com/8c1gye How to consume the web service. Could someone please help me ?
[WebGet]
meanswebrequest.Method = "GET"
. – Mahatma