1

我创建了一个如下所示的宁静网络服务

运营合同

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

邮件信息类

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

如何使用 HttpWebrequest 调用此服务?

我的服务网址

localhost/Chat/ChatService.svc/PushNotification

4

2 回答 2

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();
于 2013-04-30T12:36:18.277 回答
0

您可以省去使用的麻烦,HttpWebRequest只需使用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
于 2013-04-30T12:37:12.783 回答