一、首先建立Http的服务端,此示例的寄宿体为WindowsService,以下代码仅为WCF Restful服务代码,不包括服务启动和安装代码

1.服务契约

 /// <summary>
/// TEST
/// </summary>
[ServiceContract(Name = "IInSideContract_EnterpriseLibrary")]
public interface IInSideContract_TEST
{
/// <summary>
/// Post获取方式测试
/// </summary>
/// <param name="num1"></param>
/// <returns></returns>
[OperationContract(Name = "ElibPostTest1")]
[WebInvoke(Method = "POST", UriTemplate = "ElibPostTest1",
BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Xml)]
string ElibPostTest1(string num1); }

2.服务实现

 /// <summary>
/// TEST
/// </summary>
public class EnterpriseLibrary:IInSideContract_TEST
{
/// <summary>
/// Post获取方式测试
/// </summary>
/// <param name="num1"></param>
/// <returns></returns>
public string ElibPostTest1(string num1)
{
return num1;
}
}

3.配置文件
配置Http的服务

 <system.serviceModel>
<services>
<service name="Services.InSideService_Test"
behaviorConfiguration="GetPostBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="webBindingNoneSecurityPublic"
behaviorConfiguration="GetPostEndBehaviors"
contract="Contracts.IInSideContract_EnterpriseLibrary">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9999/InSideService_EnterpriseLibrary" />
</baseAddresses>
</host>
</service>
<bindings>
<webHttpBinding>
<binding name="webBindingNoneSecurityPublic" closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="" maxReceivedMessageSize=""
useDefaultWebProxy="false">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength=""
maxBytesPerRead="" maxNameTableCharCount="" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<webHttpBinding>
<binding name="webBindingNoneSecurityPublic" closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="" maxReceivedMessageSize=""
useDefaultWebProxy="false">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength=""
maxBytesPerRead="" maxNameTableCharCount="" />
<security mode="None" />
</binding>
</webHttpBinding>
<behaviors>
<serviceBehaviors>

二、建立POST调用WCF Restful服务的客户端

1.客户端代码

  static void Main(string[] args)
{
string url = "http://localhost:9999/InSideService_EnterpriseLibrary/ElibPostTest1"; try
{ XmlDocument doc = new XmlDocument();
doc.Load(@"D:\工作目录\TEST\test\ConsoleApplication2\bin\Debug\XMLFile1.xml");
byte[] bytes = Encoding.UTF8.GetBytes(doc.InnerXml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "application/xml";
Stream reqstream = request.GetRequestStream();
reqstream.Write(bytes, , bytes.Length);
request.Timeout = ;
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.UTF8;
StreamReader streamReader = new StreamReader(streamReceive, encoding);
string strResult = streamReader.ReadToEnd();
streamReceive.Dispose();
streamReader.Dispose();
Console.WriteLine(strResult);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}

2.Xml参数

 <?xml version="1.0" encoding="utf-8" ?>
<!--方法名、服务命名空间-->
<ElibPostTest1 xmlns="http://tempuri.org/">
<!--参数名称-->
<num1></num1>
</ElibPostTest1>

3.调用结果

WCF Restful Post调用的更多相关文章

  1. 使用多种客户端消费WCF RestFul服务(四)——Jquery篇

    Jquery篇 互联网开发中少不了各类前端开发框架,其中JQUERY就是最流行之一,本篇我们就采用JQUERY来消费WCF RestFul服务,其中用到JSON基础知识,如果有想了解的朋友,请访问:& ...

  2. 使用多种客户端消费WCF RestFul服务(三)——.net4.5篇

    .net 4.5篇 在.net 4.5下面微软提供了System.Net.Http.dll可以非常方便的使用HTTP请求(其实是用来支持Asp.Net Web Api的,不过我们可以拿过来用) 服务仍 ...

  3. [经验] - JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案

    最近在开发WSS RESTful服务的时候, 碰到了这些个纠结的问题. 在网上查找了半天, 找到n多种解决方案, 但是都是部分的, 要么是没有跨域的情况, 要么是没有post的情况, 要么不是用WCF ...

  4. JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案

    JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案 最近在开发WSS RESTful服务的时候, 碰到了这些个纠 ...

  5. WCF Restful Service Get / Post请求

    Rest 它是用于创建分布式超文本媒体的一种架构方式,我们可以通过标准的HTTP(GET,POST,PUT,DELETE)操作来构建基于面向资源的软件架构方式(Resource-Oriented Ar ...

  6. WCF Restful Service

    对 Web Services.WCF 和 Restful 的扫盲可参见:https://www.cnblogs.com/scy251147/p/3382436.html 关于之前对 WCF 的学习,可 ...

  7. Linux学习日记-WCF RestFul的部署(三)

    一.关于WCF 的部署 默认的wshttp风格的wcf是很容易部署上去的,但是这里给个建议尽量不要使用WCF的配置文件去部署尽管 我们都已经很熟悉了,在使用配置文件你会发现各种蛋疼的问题. 二.WCF ...

  8. WCF初探-11:WCF客户端异步调用服务

    前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...

  9. 构建基于WCF Restful Service的服务

    前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面的Dynamic Proxy,但是没有这种方式简便), ...

随机推荐

  1. 关于IIS权限问题(Selenium WebDriver调用出错记录)

    本地VS调试过程中用Selenium WebDriver打开FF浏览器可以正常工作,项目部署至IIS后请求调用浏览器一直提示超时,异常如下: 因为本地调试可以成功,首先排除组件版本问题和浏览器兼容问题 ...

  2. (转)SQL一次性插入大量数据

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...

  3. MongoDB开启安全认证

    MongoDB开启安全认证 注意 对MongoDB部署启用访问控制会强制执行身份验证,要求用户识别自己.当访问启用了访问控制的MongoDB部署时,用户只能执行由其角色确定的操作. 启用访问控制后,请 ...

  4. Python 输出百分比的两种方式

    Python 输出百分比的两种方式 注: 在python3环境下测试. 方式1:直接使用参数格式化:{:.2%} {:.2%}: 显示小数点后2位 显示小数点后2位: >>> pri ...

  5. Enumerable的判断是否包含某个元素

    // 通过使用默认的相等比较器(即纯粹的对象比较)确定序列是否包含指定的元素. public static bool Contains<TSource>(this IEnumerable& ...

  6. js中删除数组中某一项的方法

    1:js中的splice方法 splice(index,len,[item])    注释:该方法会改变原始数组. splice有3个参数,它也可以用来替换/删除/添加数组内某一个或者几个值 inde ...

  7. 上下行分流下行负载方式和能ping通但不能打开

    1 下行线路负载方式选择  目的端口+协议  否则有可能出现微信443端口图片打不开的情况. 2.彭ping通但是打不开的情况下将上行线路mtu值改小 由1500改为1450

  8. Spring入门5.事务管理机制

    Spring入门5.事务管理机制 20131126 代码下载 : 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 回顾之前的知识,Spring 最为核心的两个部分 ...

  9. Algorithm1: 全排列

    全排列 思想:      这是一个全排列问题,需要使用递归实现,将数组中的所有元素和第一个元素交换,求后面n-1个元素的全排列.      按照这个条件递归下去,知道元素的个数只有一个的时候,输出所有 ...

  10. LeetCode OJ:Gas Station(加油站问题)

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...