如何在C#中使用WCF REST服务?

2023-12-31

我的合同详细信息如下。我使用 Json 响应和请求格式,还使用 ​​POST 方法。如何用 C# 编写客户端来使用此服务。

[OperationContract()]
[WebInvoke(UriTemplate = "/RESTJson_Sample1_Sample1Add", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int RESTJson_Sample1_Sample1Add(Int32 a, Int32 b, Int32 c);

尝试如下:

   [OperationContract()]
   [WebInvoke(UriTemplate = "/RESTJson_Sample1_Sample1Add?A=a&B=b&C=c", Method = "POST",  
     RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,   
     BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    int RESTJson_Sample1_Sample1Add(Int32 a, Int32 b, Int32 c);

       var httpWebRequest = (HttpWebRequest)WebRequest.Create("/RESTJson_Sample1_Sample1Add?A=a&B=b&C=c");
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = methodType;//POST/GET
        string responseText = "";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(body);//any parameter
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            responseText = streamReader.ReadToEnd();
        }
        return responseText;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在C#中使用WCF REST服务? 的相关文章

随机推荐

Powered by Hwhale