3人对饮

导航

 
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

    }

 

posted on 2014-09-24 12:26  3人对饮  阅读(815)  评论(0编辑  收藏  举报