Introduction
This article helps you call Restful service using HttpWebRequest and Post Data to a particular service.
Our Restful Service
Contract
[ServiceContract]
public interface IOrionChatService
{
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Json, UriTemplate = "/PushNotification")]
[OperationContract]
void MyServiceMethod(PostedInformation postedInformations);
}
Service Implementation
public void MyServiceMethod(PostedInformation postedInformations)
{
//do implemention code here
}
DataContract
[DataContract]
public class PostedInformation
{
[DataMember]
public List<string> To { get; set; }
[DataMember]
public string SenderEmail { get; set; }
[DataMember]
public string Subject { get; set; }
[DataMember]
public string SenderFullName { get; set; }
}
Calling our created Restful service from a Console application using HttpWebRequest
//Create a new object of the class that we want to post
PostedInformation postedInformations = new PostedInformation ()
{
SenderEmail = "aaa@bbb.com",
SenderFullName = "test",
To = new List<string>() { "ccc@eee.com" }
};
//Serialize the postedInformations to jSon using Newtonsoft.json. You can download dll from here //http://json.codeplex.com/
var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postedInformations ));
//Passyour service url to the create method
var req =
HttpWebRequest.Create("http://localhost/MyServer/YourServiceName.svc/MyServiceMethod");
req.ContentType = "application/json";
req.ContentLength = dataToSend.Length;
req.Method = "POST";
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
var response = req.GetResponse();
Enjoy......
This article helps you call Restful service using HttpWebRequest and Post Data to a particular service.
Our Restful Service
Contract
[ServiceContract]
public interface IOrionChatService
{
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,ResponseFormat = WebMessageFormat.Json, UriTemplate = "/PushNotification")]
[OperationContract]
void MyServiceMethod(PostedInformation postedInformations);
}
Service Implementation
public void MyServiceMethod(PostedInformation postedInformations)
{
//do implemention code here
}
DataContract
[DataContract]
public class PostedInformation
{
[DataMember]
public List<string> To { get; set; }
[DataMember]
public string SenderEmail { get; set; }
[DataMember]
public string Subject { get; set; }
[DataMember]
public string SenderFullName { get; set; }
}
Calling our created Restful service from a Console application using HttpWebRequest
//Create a new object of the class that we want to post
PostedInformation postedInformations = new PostedInformation ()
{
SenderEmail = "aaa@bbb.com",
SenderFullName = "test",
To = new List<string>() { "ccc@eee.com" }
};
//Serialize the postedInformations to jSon using Newtonsoft.json. You can download dll from here //http://json.codeplex.com/
var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postedInformations ));
//Passyour service url to the create method
var req =
HttpWebRequest.Create("http://localhost/MyServer/YourServiceName.svc/MyServiceMethod");
req.ContentType = "application/json";
req.ContentLength = dataToSend.Length;
req.Method = "POST";
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
var response = req.GetResponse();
Enjoy......
No comments:
Post a Comment