C # Inserts a cookie when calling WebService

Source: Internet
Author: User
Tags send cookies

Soapui How to insert a cookie

SOAP inserts a cookie in the following way, click Head, click the plus sign, and then directly set it.

Insert a cookie when calling WebService in C #

Because a cookie must be taken at the time of the call in order to obtain the data successfully, and normally by referencing the service, C # generates a bunch of code, but that's just a shell, and there's no place to set cookies. On the internet to find a lot of information, and finally found a way to summarize, so that other people less detours.

Because we are the client, the server can not control, so it is not possible to find those settings on the Internet, what cookies are enabled, it is useless, unless you are developing a server-side + client. Here we mainly discuss the caller, I have only one WSDL address, the server can not control the situation.

The client (web App) does not automatically send cookies to WCF. So the client has to do more work.

    1. Core in Iclientmessageinspector this interface, he has a beforesendrequest and afterreceivereply two methods.
    2. We're going to create a new class 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. Build a class Cookiemessageinspector, inherit Iclientmessageinspector, implement his beforesendrequest and afterreceivereply two methods.

      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)) {HttpReq                Uestmessage = Httprequestmessageobject as HttpRequestMessageProperty; if (string. IsNullOrEmpty (httprequestmessage.headers["Cookie")) {httprequestmessage.headers["Cooki                E "] = 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. By using VS to add a service reference, he will automatically generate a proxy class. After the new proxy class, join our new behavior

            Woksearchliteclient searchliteclient = new Woksearchliteclient ();            Cookiebehavior C = new Cookiebehavior (authentkey);            SEARCHLITECLIENT.ENDPOINT.BEHAVIORS.ADD (c);            var ret = Searchliteclient.search (searchrequest.queryparameters, searchrequest.retrieveparameters);

  

This completes the webservice call with the cookie

C # Inserts a cookie when calling WebService

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.