首页 > 代码库 > C#调用WebService时插入cookie

C#调用WebService时插入cookie

 

SOAPUI插入Cookie的方法

 

SOAP插入cookie的方法如下,点击Head,点击加号,然后直接设置就可以了。

技术分享

 

C#中调用webService时插入Cookie

 

由于调用的时候必须要带上cookie,才能成功获取到数据,而正常的通过引用服务,C#会生成一堆代码,但那只是个空壳子,并没有设置cookie的地方。在网上找了很多资料,最后找到了方法,在此总结一下,以便其他人少走弯路。

 

由于我们是客户端,服务端无法控制,所以网上找到那些设置config,什么启用cookie啊,都是没用的,除非你是正在开发的是服务端+客户端。这里我们主要讨论的是调用方,我只有一个wsdl的地址,服务端不可控制的情况。

 

客户端(web应用)并不会自动发送cookie到wcf。所以客户端还得做更多的工作

 

 

  1. 核心在IClientMessageInspector 这个接口,他有BeforeSendRequest和AfterReceiveReply两个方法。
  2. 我们要新建一个类CookieBehavior.cs

     

    public class CookieBehavior : IEndpointBehavior    {        private string SID { get; set; }        public CookieBehavior(string pSid)        {            SID = pSid;        }        public void Validate(ServiceEndpoint endpoint)        {            return;        }        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)        {            return;        }        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)        {            return;        }        public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)        {            behavior.MessageInspectors.Add(new CookieMessageInspector(SID));        }    }

  

 

  1. 建立一个类CookieMessageInspector,继承IClientMessageInspector,实现他的BeforeSendRequest和AfterReceiveReply两个方法。

     

      public class CookieMessageInspector : IClientMessageInspector    {        private string SID { get; set; }        public CookieMessageInspector(string pSid)        {            SID = pSid;        }        public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)        {            var cookie = "SID=" + SID;            HttpRequestMessageProperty httpRequestMessage;            object httpRequestMessageObject;            if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))            {                httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;                if (string.IsNullOrEmpty(httpRequestMessage.Headers["Cookie"]))                {                    httpRequestMessage.Headers["Cookie"] = cookie;                }            }            else            {                httpRequestMessage = new HttpRequestMessageProperty();                httpRequestMessage.Headers.Add("Cookie", cookie);                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);            }            return null;        }        public void AfterReceiveReply(ref Message reply, object correlationState)        {            return;                    }    }

      

     

     

  2. 通过vs添加服务引用,他会自动生成一个代理类。在new这个代理类之后,加入我们新建的behavior

 

            WokSearchLiteClient searchLiteClient = new WokSearchLiteClient();            CookieBehavior c = new CookieBehavior(authentKey);            searchLiteClient.Endpoint.Behaviors.Add(c);            var ret = searchLiteClient.search(searchRequest.queryParameters, searchRequest.retrieveParameters);

  

 

这样就完成了,带cookie的webService调用

C#调用WebService时插入cookie