wcf客户端 cookie
public class CookieBehavior:IEndpointBehavior
{ private string _cookie; #region 构造函数 重载+2 public CookieBehavior() { } public CookieBehavior(string cookie)
{
_cookie = cookie;
} #endregion #region 接口成员 #region 未实现的接口成员,调用将抛异常
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
} public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
} public void Validate(ServiceEndpoint endpoint)
{
}
#endregion #region 在终结点范围内实现客户端的修改或扩展
/// <summary>
/// 在终结点范围内实现客户端的修改或扩展
/// </summary>
/// <param name="endpoint">要自定义的终结点</param>
/// <param name="clientRuntime">要自定义的客户端运行时</param>
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
SharedCookieMessageInspector insperctor = new SharedCookieMessageInspector(_cookie);
//客户的运行时的消息检查器集合加入自定义的消息检查器
clientRuntime.MessageInspectors.Add(insperctor);
}
#endregion #endregion
实现cookieBehavior类继承IEndpointBehavior接口,为ChnnelFactory添加行为
factory.Endpoint.Behaviors.Add(behavior);
public class SharedCookieMessageInspector:IClientMessageInspector
{
public static string soapCookie;//存储服务端返回的cookie值 #region 构造函数 public SharedCookieMessageInspector(string cookie)
{
//赋值属性
soapCookie = cookie;
}
#endregion #region 实现接口成员 #region 在将请求消息发送到服务之前,启用消息的检查或修改
/// <summary>
/// 在将请求消息发送到服务之前,向消息中写入cookie
/// </summary>
/// <param name="request">要发送给服务的消息</param>
/// <param name="channel">客户端对象通道</param>
/// <returns>唯一状态</returns>
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
string cookie = CookieMe.GetCookie();
HttpRequestMessageProperty reqMessage;
object httpRequestMessageObject;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
reqMessage = httpRequestMessageObject as HttpRequestMessageProperty;
if (string.IsNullOrEmpty(reqMessage.Headers["Cookie"]))
{
reqMessage.Headers["Cookie"] = cookie;
}
}
else
{
reqMessage = new HttpRequestMessageProperty();
reqMessage.Headers.Add("Cookie",cookie);
request.Properties.Add(HttpRequestMessageProperty.Name,reqMessage);
} return null;//不使用相关状态,则为null
}
#endregion #region 在收到回复消息之后将它传递回客户端应用程序之前,启用消息的检查或修改
/// <summary>
/// 在收到回复消息之后,存储 cookie。
/// </summary>
/// <param name="reply">要转换为类型并交回给客户端应用程序的消息</param>
/// <param name="correlationState">关联状态数据</param>
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
//提供对http响应的访问,以便访问和响应为 HTTP 协议请求提供的附加信息
HttpResponseMessageProperty httpResponse =
reply.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
if (httpResponse != null)
{
soapCookie = httpResponse.Headers["Set-Cookie"];//从服务端响应消息头中提取cookie
if (!string.IsNullOrEmpty(soapCookie))
{
CookieMe.SaveCookie(soapCookie);
} } } #endregion #endregion }
wcf客户端 cookie的更多相关文章
- WCF 基于Cookie的登录验证回传问题的解决
参考资料: http://www.cnblogs.com/czcz1024/p/3333138.html http://megakemp.com/2009/02/06/managing-shared- ...
- WCF常见问题(1) -- WebService/WCF Session Cookie
原文:WCF常见问题(1) -- WebService/WCF Session Cookie 在.net 3.0推出WCF之前使用的WebService,有的应用有使用Session保持一些信息,在不 ...
- 终于解决:升级至.NET 4.6.1后VS2015生成WCF客户端代理类的问题
在Visual Studio 2015中将一个包含WCF引用的项目的targetFramework从4.5改为4.6.1的时候,VS2015会重新生成WCF客户端代理类.如果WCF引用配置中选中了&q ...
- nginx负载均衡之基于客户端cookie的会话保持
通过ip_hash做会话保持有一定的缺陷,这个是通过客户端ip来实现.同一个网络下众多客户端访问服务器会被扔到同一台机器,再或者是CDN也 会导致负载不均衡.所以要实现通过客户端cookie实现,包括 ...
- WCF初探-10:WCF客户端调用服务
创建WCF 服务客户端应用程序需要执行下列步骤: 获取服务终结点的服务协定.绑定以及地址信息 使用该信息创建 WCF 客户端 调用操作 关闭该 WCF 客户端对象 WCF客户端调用服务存在以下特点: ...
- WCF初探-11:WCF客户端异步调用服务
前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...
- WCF初探-12:WCF客户端异常处理
前言: 当我们打开WCF基础客户端通道(无论是通过显式打开还是通过调用操作自动打开).使用客户端或通道对象调用操作,或关闭基础客户端通道时,都会在客户端应用程序中出现异常.而我们知道WCF是基于网络的 ...
- WCF初探-13:WCF客户端为双工服务创建回调对象
前言: 在WCF初探-5:WCF消息交换模式之双工通讯(Duplex)博文中,我讲解了双工通信服务的一个应用场景,即订阅和发布模式,这一篇,我将通过一个消息发送的例子讲解一下WCF客户端如何为双工服务 ...
- 生产WCF客户端类文件的命令格式
生产WCF客户端类文件的命令格式: svcutil.exe net.tcp://127.0.0.1:8732/ChromaMI.Remote.ConfigService/RemoteConfigSer ...
随机推荐
- 斯坦福NG机器学习课程:Anomaly Detection笔记
Anomaly Detection Problem motivation: 首先描写叙述异常检測的样例:飞机发动机异常检測 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...
- `npm install --save --save-exact react-native` failed
当你使用命令行创建一个项目 react-native init 项目名称 后出现以下错误 Installing react-native package from npm... /bin/sh: ...
- C#核编之System.Environment类
在前面的例子中用来了Environment.GetCommandLineArgs()这个方法,这个方法就是获取用户的命令行输入,是Environment类的方法之一,该方法的返回值是string[] ...
- Set无序怎么办?
在JAVA中,提供多种不同的结构来组织对象,Set(集合)是其中的一种,本身是一个接口,其迭代时的顺序取决于其具体实现. 典型的实现包括: HashSet:哈希表是通过使用称为散列法的机制来存储信息的 ...
- applicationContext.xml 配置文件的存放位置
eb.xml中classpath:和classpath*: 有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中 ...
- Javascript基础示例:用JS写简易版贪吃蛇(面向对象)
废话不多说,代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- 解析:用 CSS3 和 JavaScript 制作径向动画菜单
原作者的解析(英文):http://creative-punch.net/2014/02/making-animated-radial-menu-css3-javascript/ 原作者的解析(译文) ...
- C# JSON各种查找法
http://blog.csdn.net/yangxiaojun9238/article/details/8490319
- 你的阅读造就了你 You are what you read
在豆瓣上看到的一篇很有思想和正能量的文章,在这里请允许我用原创的方式来呈现给大家.如果你是在校的大学生或者研究生博士生,这篇文章会让你有很多的共鸣.如果你已真正的踏入这个社会,也将受益匪浅. 电脑 ...
- 走进C标准库(3)——"stdio.h"中的getc和ungetc
接前文. 再来看看getc和ungetc的实现.在看这两个函数的实现之前,我们先来想一想这两个函数分别需要做的工作. int getc(FILE *stream) 说明:函数getc从stream指向 ...