0

我使用 WCF REST 服务模板 40(CS) 创建了一个网站,它具有如下服务方法:

[WebInvoke(UriTemplate = "CTNotification", Method = "POST", ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json)]      

public string CTNotification(Stream contents)

如何将 json 传递给它?我无法将服务参数设为字符串,因为客户端会将 json 作为流发送。如何使用 C# 对此服务方法进行后调用,内容类型 = application/json

问候, 阿西夫·哈米德

4

1 回答 1

0

我不确定您是同步执行此操作还是同步执行此操作,但这是传递参数的方法。实施可能会根据客户的需求而有所不同

    HttpWebRequest httpWReq =  (HttpWebRequest)WebRequest.Create(ServiceAddress + "CTNotification/");

                //parameters
                byte[] data = Encoding.UTF8.GetBytes(jsonString);

                httpWReq.Method = "POST";
                httpWReq.ContentType = "application/x-www-form-urlencoded"; 
                httpWReq.ContentLength = data.Length;
                httpWReq.Timeout = 1000;

                using (Stream newStream = httpWReq.GetRequestStream())
                {
                    newStream.Write(data, 0, data.Length);
                }
于 2013-04-25T20:26:50.073 回答