zoukankan      html  css  js  c++  java
  • WCF Restful 服务 Get/Post请求

    Restful  Get方式请求:

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

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

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

    1.返回值得类型会自行序列化成XML显示在页面

    2.

    http://localhost:10718/Service1.vsc/Get?StrA=A&StrB=B&StrC=C

    代码实现:

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

    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]
           //UriTemplate = "Get?StrA={StrA}&StrB={StrB}&StrC={StrC}",
    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方式请求:

  • 相关阅读:
    python:推导式套路
    重大技术需求征集系统八稿开发进度(8)
    重大技术需求征集系统八稿开发进度(7)
    重大技术征集系统第八稿开发进度(6)
    hbase的基本操作
    重大技术征集系统八稿(5)
    重大技术八稿开发记录(4)
    重大技术八稿开发记录(3)
    重大技术八稿开发记录(2)
    重大技术八稿开发记录(1)
  • 原文地址:https://www.cnblogs.com/yf2011/p/4011133.html
Copyright © 2011-2022 走看看