如何在 C# 中使用 WCF REST 服务? [英] How to consume WCF REST Service in C#?

查看:39
本文介绍了如何在 C# 中使用 WCF REST 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的合同详情如下.我正在使用 Json 响应和请求格式,也使用 POST 方法.如何在c#中编写一个客户端来使用这个服务.

My contract details are below. I am using Json response and request format and also using POST method. How to write a client to consume this service in 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);

推荐答案

要使用 WCF Restful 服务,无需在 Web.config 中进行更改.找到下面的代码来消费WCF restful服务的POST方法.

To consume WCF Restful service, there is no need of making changes in Web.config. find the below code to consume the POST method of WCF restful service.

        DataContractJsonSerializer objseria = new DataContractJsonSerializer(typeof(StudentDetails));
        MemoryStream mem = new MemoryStream();
        objseria.WriteObject(mem, stu);
        string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
        WebClient webClient = new WebClient();
        webClient.Headers["Content-type"] = "application/json";
        webClient.Encoding = Encoding.UTF8;
        webClient.UploadString("http://localhost:62013/Service1.svc/ADDStudent", "POST", data);

参考链接

这篇关于如何在 C# 中使用 WCF REST 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆