C#調用WebService時插入cookie

 

SOAPUI插入Cookie的方法

 

SOAP插入cookie的方法以下,點擊Head,點擊加號,而後直接設置就能夠了。web

 

C#中調用webService時插入Cookie

 

因爲調用的時候必需要帶上cookie,才能成功獲取到數據,而正常的經過引用服務,C#會生成一堆代碼,但那只是個空殼子,並無設置cookie的地方。在網上找了不少資料,最後找到了方法,在此總結一下,以便其餘人少走彎路。cookie

 

因爲咱們是客戶端,服務端沒法控制,因此網上找到那些設置config,什麼啓用cookie啊,都是沒用的,除非你是正在開發的是服務端+客戶端。這裏咱們主要討論的是調用方,我只有一個wsdl的地址,服務端不可控制的狀況。spa

 

客戶端(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調用blog

相關文章
相關標籤/搜索