WCF Restful Service Get / Post请求
Rest 它是用于创建分布式超文本媒体的一种架构方式,我们可以通过标准的HTTP(GET,POST,PUT,DELETE)操作来构建基于面向资源的软件架构方式(Resource-Oriented Architecture (ROA))。它是独立于任何技术或者平台的,所以人们经常将符合这种操作规范的服务称为“RESTful services”。因为WCF能够构建符合这种规范的服务,所以我们经常称之为 WCF Restful Services。
由于传统的WCF Service可以使用tcp,net.msmq,http等协议进行数据交换,并且采用了RPC(Remote Procedure Call)的工作方式,客户端需要添加对服务端的引用才能完成。但是WCF Restful Service完全使用Http协议来进行,并且无需添加客户端引用,所以方便很多。
WebInvoke
其中,Method 方法主要是表明可以接受客户端的请求类型,这里有四种:GET,POST,PUT,DELETE,其中GET为请求数据,POST为更新数据,PUT为新增数据,DELETE代表着删除数据。
然后ResponseFormat 则代表着返回的数据组织,如果是Json则表明客户端会接收到Json数据,如果是XML则表明客户端会接收到XML组织的数据。BodyStyle 代表返回数据的包装对象,如果是Bare则表明数据无任何包装,原生数据返回;如果是Wrapped则表明数据会在最外层包装一个当前函数名称加上Result的套。比如对于Delete对象,则会返回 DeleteResult:{******},会造成DataContractJsonSerializer无法进行反序列化。
UriTemplate 主要用于指定操作的URI路径,只要用户输入了合法路径并采用了正确的请求方式,就会触发该函数。
最后说到的就是URI后面跟的参数的问题,由于函数只能接受string类型的,所以如果传入参数是string类型,则可以使用UriTemplate = "{bookID}"的路径,反之,则需要加上/?param1={paramname}的方式
服务端

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.Serialization;
5 using System.ServiceModel;
6 using System.ServiceModel.Web;
7 using System.Text;
8
9 namespace IISWCF
10 {
11 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
12 [ServiceContract]
13 public interface IService1
14 {
15
16 #region TEST
17
18 [OperationContract(Name = "TestPost")]
19 [WebInvoke(Method = "POST", UriTemplate = "TestPost/{str}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
20 UserInfo TestPost(string str);
21
22 [OperationContract, WebInvoke(Method = "GET", UriTemplate = "TestGet1")]
23 string TestGet1();
24
25 [OperationContract(Name = "TestGet2")]
26 [WebGet(UriTemplate = "TestGet2/{Code}/{Card}",
27 BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
28 UserInfo TestGet2(string Code, string Card);
29
30 #endregion
31 }
32
33
34 // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
35 [DataContract]
36 public class UserInfo
37 {
38 [DataMember]
39 public string Name { get; set; }
40 [DataMember]
41 public string Code { get; set; }
42 [DataMember]
43 public string Card { get; set; }
44 }
45 }


1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.Serialization;
5 using System.ServiceModel;
6 using System.ServiceModel.Web;
7 using System.Text;
8
9 namespace IISWCF
10 {
11 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
12 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。
13 public class Service1 : IService1
14 {
15 #region TEST
16
17 public UserInfo TestPost(string str)
18 {
19 UserInfo u = new UserInfo();
20 u.Name = str;
21 return u;
22 }
23
24 public string TestGet1()
25 {
26 return "调用成功!";
27 }
28
29 public UserInfo TestGet2(string Code, string Card)
30 {
31 UserInfo u = new UserInfo();
32 u.Code = Code;
33 u.Card = Card;
34 return u;
35 }
36
37 #endregion
38
39 }
40 }


1 <?xml version="1.0" encoding="utf-8"?>
2 <configuration>
3 <system.web>
4 <compilation debug="true" targetFramework="4.0" />
5 </system.web>
6 <system.serviceModel>
7 <services>
8 <service behaviorConfiguration="GetPostBehavior" name="IISWCF.Service1">
9 <endpoint address="" behaviorConfiguration="GetPostEndBehaviors" binding="webHttpBinding"
10 contract="IISWCF.IService1">
11 </endpoint>
12 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
13 </service>
14 </services>
15 <behaviors>
16 <endpointBehaviors>
17 <behavior name="GetPostEndBehaviors">
18 <webHttp />
19 </behavior>
20 </endpointBehaviors>
21 <serviceBehaviors>
22 <behavior name="GetPostBehavior">
23 <serviceMetadata httpGetEnabled="true" />
24 <serviceDebug includeExceptionDetailInFaults="false" />
25 </behavior>
26 </serviceBehaviors>
27 </behaviors>
28 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
29 </system.serviceModel>
30 <system.webServer>
31 <modules runAllManagedModulesForAllRequests="true"/>
32 <!--
33 若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
34 在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
35 -->
36 <directoryBrowse enabled="true"/>
37 </system.webServer>
38
39 </configuration>

客户端代码

1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Net;
6 using System.Text;
7
8 namespace ClientPost
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 var url = "http://localhost:6729/Service1.svc";
15 try
16 {
17 Console.WriteLine("Post Method TestPost");
18 string parms = "张三";
19 string method = "TestPost";
20 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url + "/" + method + "/" + parms);
21 request.Method = "POST";
22 request.ContentType = "text/plain";
23 Stream requestStram = request.GetRequestStream();
24 requestStram.Close();
25 HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
26 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
27 string ReqResult = reader.ReadToEnd();
28 Console.WriteLine(ReqResult);
29 Console.WriteLine();
30 }
31 catch (Exception ex)
32 {
33 Console.WriteLine(ex.ToString());
34 }
35
36 try
37 {
38 Console.WriteLine("Get Method TestGet1");
39 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + "/TestGet1");
40 httpWebRequest.ContentType = "application/json";
41 httpWebRequest.Method = "GET";
42 httpWebRequest.Timeout = 20000;
43 HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
44 StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
45 string responseContent = streamReader.ReadToEnd();
46 httpWebResponse.Close();
47 streamReader.Close();
48 Console.WriteLine(responseContent);
49 Console.WriteLine();
50 }
51 catch (Exception ex)
52 {
53 Console.WriteLine(ex.ToString());
54 }
55
56 try
57 {
58 Console.WriteLine("Get Method TestGet2");
59 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + "/TestGet2/code123/card456");
60 httpWebRequest.ContentType = "application/json";
61 httpWebRequest.Method = "GET";
62 httpWebRequest.Timeout = 20000;
63 HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
64 StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
65 string responseContent = streamReader.ReadToEnd();
66 httpWebResponse.Close();
67 streamReader.Close();
68 Console.WriteLine(responseContent);
69 }
70 catch (Exception ex)
71 {
72 Console.WriteLine(ex.ToString());
73 }
74 Console.ReadLine();
75 }
76 }
77 }

运行结果:

源码下载WCF-Post-Get.rar
WCF Restful Service Get / Post请求的更多相关文章
- 构建基于WCF Restful Service的服务
前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面的Dynamic Proxy,但是没有这种方式简便), ...
- WCF Restful Service的服务
构建基于WCF Restful Service的服务 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面 ...
- [转]构建基于WCF Restful Service的服务
本文转自:http://www.cnblogs.com/scy251147/p/3566638.html 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添 ...
- WCF Restful Service
对 Web Services.WCF 和 Restful 的扫盲可参见:https://www.cnblogs.com/scy251147/p/3382436.html 关于之前对 WCF 的学习,可 ...
- Wcf Restful Service服务搭建
目的 使用Wcf(C#)搭建一个Restful Service 背景 最近接到一个项目,客户要求使用Restful 方式接收到数据,并对数据提供对数据的统计显示功能,简单是简单,但必须要使用Restf ...
- WCF Restful 服务 Get/Post请求
Restful Get方式请求: Restful服务 Get请求方式:http://localhost:10718/Service1.svc/Get/A/B/C http://localhost:1 ...
- [转]WCF RESTful service and WebGrid in ASP.NET MVC 5
使用WebClient调用WCF服务 流程:从View获取实体类-->序列化-->写入内存流中-->传给远端的WCF服务 Get.POST.PUT.DELETE,客户端以流的方式调用 ...
- 在IIS8.5的环境下配置WCF的Restful Service
今天在客户的环境中(Windows Server 2012 R2 + IIS 8.5)搭建Call WCF Restful Service的功能,发现了几个环境配置的问题,记录如下: 1):此环境先安 ...
- TinyFrame升级之十:WCF Rest Service注入IOC的心
由于在实际开发中,Silverlight需要调用WebService完成数据的获取,由于之前我们一直采用古老的ASMX方式,生成的代理类不仅难以维护,而且自身没有提供APM模式的调用方式,导致在Sin ...
随机推荐
- sqlmap 1.3.2.16 帮助信息
Usage: sqlmap.py [options] 选项: -h, --help 显示基本帮助信息并退出 -hh 显示高级帮助消息并退出 --version 显示程序的版本号并退出 -v VERBO ...
- Remmarguts' Date POJ - 2449 (A*搜索|k短路)
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...
- 二、网络编程-socket之TCP协议开发客户端和服务端通信
知识点:之前讲的udp协议传输数据是不安全的,不可靠不稳定的,tcp协议传输数据安全可靠,因为它们的通讯机制是不一样的.udp是用户数据报传输,也就是直接丢一个数据包给另外一个程序,就好比寄信给别人, ...
- Codeforces 1118F1 Tree Cutting (Easy Version) (简单树形DP)
<题目链接> 题目大意: 给定一棵树,树上的点有0,1,2三中情况,0代表该点无色.现在需要你将这棵树割掉一些边,使得割掉每条边分割成的两部分均最多只含有一种颜色的点,即分割后的两部分不能 ...
- POJ 1904 King's Quest (强连通分量+完美匹配)
<题目链接> 题目大意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王 ...
- iOS APP提交上架流程
转载自CocoaChina,链接地址:http://www.cocoachina.com/bbs/read.php?tid=330302 后面问题我也遇到了,参考该文章解决的 转自http://blo ...
- VirtualBox 共享文件夹设置及使用方法
工具:VirtualBox,ubuntu14.04 1.选择要设置共享文件夹的虚拟机,点击设置 点击共享文件夹,新建文件夹,选择路径 2.打开虚拟机,从上方工具栏中找到设备,然后点击安装增强功能(由于 ...
- Alpha(3/10)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- 简单总结下opacity、transparent以及rgba
这几个概念最开始学的时候只是有个大致印象,现在复习这部分的知识点,发现不仔细区分一下,还真有点混乱. 三者共同点是都和透明有关.先分着来说一下: 1.opacity用来设置元素的不透明级别,从 0.0 ...
- URAL Palindromic Contest
A. Non-palidromic cutting 考虑无解的情形:只能是形如$aaaaa$.$aaabaaa$.$abababa$这三种情况. 有解时,对于最小划分,答案必定是$1$或者$2$,判断 ...