标签:auth 获取 add png pat 添加 arc bind ntc
SOAP插入cookie的方法如下,点击Head,点击加号,然后直接设置就可以了。
由于调用的时候必须要带上cookie,才能成功获取到数据,而正常的通过引用服务,C#会生成一堆代码,但那只是个空壳子,并没有设置cookie的地方。在网上找了很多资料,最后找到了方法,在此总结一下,以便其他人少走弯路。
由于我们是客户端,服务端无法控制,所以网上找到那些设置config,什么启用cookie啊,都是没用的,除非你是正在开发的是服务端+客户端。这里我们主要讨论的是调用方,我只有一个wsdl的地址,服务端不可控制的情况。
客户端(web应用)并不会自动发送cookie到wcf。所以客户端还得做更多的工作
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)); } }
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; } }
WokSearchLiteClient searchLiteClient = new WokSearchLiteClient(); CookieBehavior c = new CookieBehavior(authentKey); searchLiteClient.Endpoint.Behaviors.Add(c); var ret = searchLiteClient.search(searchRequest.queryParameters, searchRequest.retrieveParameters);
这样就完成了,带cookie的webService调用
标签:auth 获取 add png pat 添加 arc bind ntc
原文地址:http://www.cnblogs.com/JangoJing/p/7018724.html