1

I have created a restful webservice like below

Opertation Contract

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json, UriTemplate = "/PushNotification")]
        [OperationContract]
        void PushNotification(MailInformation mailInformations);

MailInformations class

 [DataContract]
    public class MailInformation
    {
        [DataMember]
        public List<string> To { get; set; }
        [DataMember]
        public string SenderEmail { get; set; }
        [DataMember]
        public string Subject { get; set; }
    }

How can i call this service using HttpWebrequest ?

My Service Url

localhost/Chat/ChatService.svc/PushNotification

Jameel Moideen
  • 7,542
  • 12
  • 51
  • 79

2 Answers2

6
MailInformation mi = new MailInformation(){
    SenderEmail = "aaa@bbb.com",
    Subject = "test",
    To = new List<string>(){"ccc@eee.com"}
};

var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(mi));

var req = HttpWebRequest.Create("http://localhost/Chat/ChatService.svc/PushNotification");

req.ContentType = "application/json";
req.ContentLength = dataToSend.Length;
req.Method = "POST";
req.GetRequestStream().Write(dataToSend,0,dataToSend.Length);

var response = req.GetResponse();
I4V
  • 34,891
  • 6
  • 67
  • 79
0

You can save yourself the hassle of using HttpWebRequest and just use RestSharp.

var client = new RestClient("http://localhost");
var request = new RestRequest("Chat/ChatService.svc/PushNotification");
RestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
Chris S
  • 64,770
  • 52
  • 221
  • 239
  • @JEMI here's an example: http://stackoverflow.com/questions/6312970/restsharp-json-parameter-posting – Chris S Apr 30 '13 at 14:38
  • @JEMI you can also post the object directly if you want: http://stackoverflow.com/questions/9966521/restsharp-post-object-to-wcf – Chris S Apr 30 '13 at 14:58