码迷,mamicode.com
首页 > 其他好文 > 详细

WCF Restful 服务 Get/Post请求

时间:2014-10-08 17:11:05      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   strong   

Restful  Get方式请求:

Restful服务 Get请求方式:http://localhost:10718/Service1.svc/Get/A/B/C

http://localhost:10718/Service1.svc 服务地址;Get 方法名;A,B,C分别为三个String参数的值。

请求所得数据将在页面显示如图:

bubuko.com,布布扣

 

代码实现:

简单示例:一个查询方法,获取三个参数,并将得到的参数显示到页面

1.接口契约

 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 WcfService1
10 {
11     [ServiceContract]
12     public interface IService1
13     {
14         [OperationContract]
15         [WebGet(UriTemplate = "Get/{StrA}/{StrB}/{StrC}",
16             BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
17         string GetData(string StrA,string StrB,string StrC);
18     }
19 
20 }

 

2.接口服务

 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 WcfService1
10 {
11     [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
12     public class Service1 : IService1
13     {
14         public string GetData(string StrA, string StrB, string StrC)
15         {
16             return string.Format("You entered: A:{0},B:{1},C:{2}", StrA, StrB, StrC);
17         }
18     }
19 }

 

3. 配置文件

 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="WcfService1.Service1">
 9         <endpoint address="" behaviorConfiguration="GetPostEndBehaviors" binding="webHttpBinding"
10                   contract="WcfService1.IService1">
11           <identity>
12             <dns value="localhost" />
13           </identity>
14         </endpoint>
15         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
16         <host>
17           <baseAddresses>
18             <add baseAddress="http://127.0.0.1:80/Service" />
19           </baseAddresses>
20         </host>
21       </service>
22     </services>
23     <bindings>
24       <webHttpBinding>
25         <binding name="GetPostServiceBinding">
26           <security mode="None"/>
27         </binding>
28       </webHttpBinding>
29     </bindings>
30     <behaviors>
31       <endpointBehaviors>
32         <behavior name="GetPostEndBehaviors">
33           <webHttp />
34         </behavior>
35       </endpointBehaviors>
36       <serviceBehaviors>
37         <behavior name="GetPostBehavior">
38           <serviceMetadata httpGetEnabled="true" />
39           <serviceDebug includeExceptionDetailInFaults="false" />
40         </behavior>
41         <behavior>
42           <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
43           <serviceMetadata httpGetEnabled="true"/>
44           <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
45           <serviceDebug includeExceptionDetailInFaults="false"/>
46         </behavior>
47       </serviceBehaviors>
48     </behaviors>
49     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
50   </system.serviceModel>
51  <system.webServer>
52     <modules runAllManagedModulesForAllRequests="true"/>
53   </system.webServer>
54 </configuration>

 

Restful  Post方式请求:

 

WCF Restful 服务 Get/Post请求

标签:style   blog   http   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/yf2011/p/4011133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!