1

以下のような安らかなWebサービスを作成しました

運営契約

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

MailInformation クラス

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

HttpWebrequest を使用してこのサービスを呼び出すにはどうすればよいですか?

私のサービスのURL

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

使用の手間を省き、 RestSharpHttpWebRequestを使用するだけです。

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 に答える