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.
- Core in Iclientmessageinspector this interface, he has a beforesendrequest and afterreceivereply two methods.
- 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)); } }
- 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; } }
- 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