WCF Restful Post调用
一、首先建立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调用的更多相关文章
- 使用多种客户端消费WCF RestFul服务(四)——Jquery篇
Jquery篇 互联网开发中少不了各类前端开发框架,其中JQUERY就是最流行之一,本篇我们就采用JQUERY来消费WCF RestFul服务,其中用到JSON基础知识,如果有想了解的朋友,请访问:& ...
- 使用多种客户端消费WCF RestFul服务(三)——.net4.5篇
.net 4.5篇 在.net 4.5下面微软提供了System.Net.Http.dll可以非常方便的使用HTTP请求(其实是用来支持Asp.Net Web Api的,不过我们可以拿过来用) 服务仍 ...
- [经验] - JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案
最近在开发WSS RESTful服务的时候, 碰到了这些个纠结的问题. 在网上查找了半天, 找到n多种解决方案, 但是都是部分的, 要么是没有跨域的情况, 要么是没有post的情况, 要么不是用WCF ...
- JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案
JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案 最近在开发WSS RESTful服务的时候, 碰到了这些个纠 ...
- WCF Restful Service Get / Post请求
Rest 它是用于创建分布式超文本媒体的一种架构方式,我们可以通过标准的HTTP(GET,POST,PUT,DELETE)操作来构建基于面向资源的软件架构方式(Resource-Oriented Ar ...
- WCF Restful Service
对 Web Services.WCF 和 Restful 的扫盲可参见:https://www.cnblogs.com/scy251147/p/3382436.html 关于之前对 WCF 的学习,可 ...
- Linux学习日记-WCF RestFul的部署(三)
一.关于WCF 的部署 默认的wshttp风格的wcf是很容易部署上去的,但是这里给个建议尽量不要使用WCF的配置文件去部署尽管 我们都已经很熟悉了,在使用配置文件你会发现各种蛋疼的问题. 二.WCF ...
- WCF初探-11:WCF客户端异步调用服务
前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...
- 构建基于WCF Restful Service的服务
前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面的Dynamic Proxy,但是没有这种方式简便), ...
随机推荐
- angular之自定义 directive
1,指令的创建至少需要一个带有@Directive装饰器修饰的控制器类.@Directive装饰器指定了一个选择器名称,用于指出与此指令相关联的属性的名字. 2,创建一个highlight.direc ...
- [mybatis]Record与Example的用法
一.Record 一个Record是一个Dao对象(继承Mapper接口),tkmybatis会将record自动映射成sql语句,record中所有非null的属性都作为sql语句,如: 映射的sq ...
- IceScrum敏捷开发工具的安装文档-官方最新版
Welcome to the iceScrum iceScrum install guide. If you don’t want to manage your own iceScrum instal ...
- 028——VUE中事件修饰符once
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 二十四、DBMS_SQL
1.概述 1) 在整个程序的设计过程中,对游标的操作切不可有省略的部分,一旦省略其中某一步骤,则会程序编译过程既告失败,如在程序结尾处未对改游标进行关闭操作,则在再次调用过程时会出现错误. 2) db ...
- SCM-MANAGER-禁用用户
用管理远用户登录到scm-manager的管理界面http://*.*.*.*:8081/ 设置目标用户为禁用 验证 非 “active” 状态 目标用户客户端不能pull 一直提示登录
- LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))
Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...
- Draggable拖动
Draggable(拖动)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 EasyUI中Draggable(拖动)组件的使用方法,这个组件不依赖于其他组件. 1.加载方式 / ...
- gradle Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.crea
gradle Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.crea ...
- eclipse 生成发布的apk (signed zipalign过程)
在发布apk到appstore过程中,上传的apk需要先signed(先生成keystore和key)并zipalign.可按照以下步骤来完成:1. 创建一个keystore和key(右键eclips ...