首页 技术 正文
技术 2022年11月8日
0 收藏 423 点赞 1,872 浏览 2257 个字

SOAPUI插入Cookie的方法

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

C#调用WebService时插入cookie

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调用

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854