jmeter - 第二次发布请求未使用在我的登录请求中创建的JSESSIONID [英] jmeter - second post request is not using the JSESSIONID created on my log in post request

查看:242
本文介绍了jmeter - 第二次发布请求未使用在我的登录请求中创建的JSESSIONID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地为我的登录终端发送了一个POST请求,并创建了一个JSESSIONID。我知道JSESSIONID由我在线程顶部的HTTP Cookie管理器保存,因为我看到它在我的线程中的几个GET请求中使用。

I was successfully able to send a POST request for my log in end point and a JSESSIONID was created. I know the JSESSIONID is kept by the HTTP Cookie Manager that I have at the top of my thread because I see it being used on several GET requests I have in my thread.

但是当我尝试POST请求时,它不使用JSESSIONID并创建自己的ID。以下是我的设置:

But when I attempt a POST request it does not use the JSESSIONID and creates its own ID. Below are my settings:

协议:https
方法:POST
-Use KeppAlive
{json:params}

Protocol: https Method: POST -Use KeppAlive {"json":"params"}

取样器结果:
线程名称:sim test 1-1
示例开始日期:2014-02-18 15 :42:42 EST
加载时间:95
延迟:95
大小(字节):239
标头大小(字节):239
主体大小(字节):0
样本数:1
错误数:0
回复代码:302
回应邮件:找到

Sampler Result: Thread Name: sim test 1-1 Sample Start: 2014-02-18 15:42:42 EST Load time: 95 Latency: 95 Size in bytes: 239 Headers size in bytes: 239 Body size in bytes: 0 Sample Count: 1 Error Count: 0 Response code: 302 Response message: Found

回复标题:
HTTP / 1.1 302找到
服务器:Apache-Coyote / 1.1
Set-Cookie:JSESSIONID = F16BF82FD28A84F6E28DDE30DECDC48C;路径= /;安全; HttpOnly
位置:https://api.zzzz com / site / needsAuth
内容长度:0
日期:星期二,2014年2月18日20:42:42 GMT

Response headers: HTTP/1.1 302 Found Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=F16BF82FD28A84F6E28DDE30DECDC48C; Path=/; Secure; HttpOnly Location: https : //api.zzzz com/site/needsAuth Content-Length: 0 Date: Tue, 18 Feb 2014 20:42:42 GMT

HTTPSampleResult字段:
ContentType:
DataEncoding:null

HTTPSampleResult fields: ContentType: DataEncoding: null

请求
POST https://api.zzz com / rest / members / 347 / passengers

Request: POST https : //api.zzz com/rest/members/347/passengers

POST数据:
{relationshipToMember:null, authorizedToBook:false,authorizedToFly:true,authorizedToGetInfo:false,passenger:{firstName:Mighty,middleName:null,lastName:Max,dateOfBirth:1380857200000 }

POST data: {"relationshipToMember":null,"authorizedToBook":false,"authorizedToFly":true,"authorizedToGetInfo":false,"passenger":{"firstName":"Mighty","middleName":null,"lastName":"Max","dateOfBirth": 1380857200000}}

[no cookies]

[no cookies]

请求标题:
连接:keep-alive
内容类型:application / json
内容长度:204
主机:api.zzz.com
用户代理:Apache-HttpClient / 4.2.6(java 1.5)

Request Headers: Connection: keep-alive Content-Type: application/json Content-Length: 204 Host: api.zzz.com User-Agent: Apache-HttpClient/4.2.6 (java 1.5)

注意:我不知道我是否正确地执行了我的GET请求,但它们正在运行。他们有Follow Redirects,它首先进入http://api.zzz ... - 302 Found,然后去https://api.zzz ...

-Note: I don't know if I am doing my GET requests correctly but they are working. They have Follow Redirects on and it first goes to http : //api.zzz... - 302 Found, then goes https : //api.zzz...

编辑:
我发现这正是我的问题: Apache JMeter的Cookie管理器不会将cookie添加到POST请求中,但如果有人能详细说明这些步骤,我不明白他的解决方案。

edit: I found this which is exactly my problem: Cookie Manager of Apache JMeter doesn't add the cookie to POST request, but I do not understand his solution if anyone can elaborate on the steps.

更新
解决:我必须保留与登录POST相同的服务器名称并更改路径

UPDATE Solved: I had to keep the same Server Name as my Log in POST and change my Path

推荐答案

更新!!!

没有注意到你的 https 协议。

Haven't noticed your https protocol.

您只需在 CookieManager.save.cookies = true 属性> jmeter.properties 文件或将其作为参数添加到JMeter命令行中

All you need is to set either CookieManager.save.cookies=true property in your jmeter.properties file or add it as an argument to JMeter command line as

jmeter -JCookieManager.save.cookies=true -n -t path_to_jmx_script.jmx -l path_to_log_file.jtl

剩下剩下的我的回复,以防万一其他人需要它作为如何跨不同线程组共享JMeter Cookie的指南。

Leaving the rest of my response just in case anyone else will need it as a guide on how to share JMeter Cookies across different Thread Groups.

我无法使用以下场景在我的环境中重现您的用例:

I'm not able to reproduce your use case in my environment using following scenarios:


  • HTTP Cookie管理器位于测试计划下(与线程组相同)

  • HTTP Cookie管理器位于线程组下(与采样器相同)

如果将HTTP Cookie管理器添加为Login请求的子节点,则它只能重现。如果是你的情况 - 将它向上移动1级以扩大它的范围。

It's only reproducible if HTTP Cookie Manager added as a child of Login request. If it's your case - move it up 1 level to broaden it's scope.

如果由于某种原因它没有帮助 - 请参阅下面的可能的解决方法详细信息。

If for some reason it doesn't help - see below for possible workaround details.

您的回复代码 204 对我来说听起来不是一个错误。我想如果基于cookie的身份验证有问题,服务器宁愿回复类似 401 403 的内容。

Your response code 204 doesn't sound like an error to me. I guess that the server would rather respond with something like 401 or 403 if there were problems with cookie-based authentication.

如果你明确需要设置cookie,它仍然可以通过ie Beanshell来完成

If you explicitly need to set cookie it still can be done via i.e. Beanshell

你需要这样做以下:


  1. 如果你要在不同的线程组之间共享cookie或者因为任何其他原因需要它们作为JMeter变量设置 CookieManager.save.cookies = true 属性在 jmeter.properties 文件中或在JMeter启动期间指定为 jmeter -JCookieManager.save.cookies = true

  2. 使用以下代码将Beanshell Post Processor添加到您的登录请求中:

  1. If you're going to share cookies between different thread groups or need them as JMeter variables for any other reason set CookieManager.save.cookies=true property either in jmeter.properties file or specify it during JMeter startup as jmeter -JCookieManager.save.cookies=true
  2. Add Beanshell Post Processor to your Login Request with following code:

import org.apache.jmeter.protocol.http.control.CookieManager;

CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();

props.put("cookiecount", String.valueOf(manager.getCookieCount()));

for (int i = 0; i < manager.getCookieCount(); i++) {
    props.put("cookie_name" + i, manager.get(i).getName());
    props.put("cookie_value" + i, manager.get(i).getValue());
    props.put("cookie_domain" + i, manager.get(i).getDomain());
    props.put("cookie_path" + i, manager.get(i).getPath());
    props.put("cookie_expires" + i, String.valueOf(manager.get(i).getExpires()));
    props.put("cookie_secure" + i, String.valueOf(manager.get(i).getSecure()));
}


  • 将Beanshell预处理器添加到 POST 请求以下代码:

    import org.apache.jmeter.protocol.http.control.CookieManager;
    import org.apache.jmeter.protocol.http.control.Cookie;
    import org.apache.jmeter.testelement.property.JMeterProperty;
    
    
    CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
    
    int count = Integer.parseInt(props.getProperty("cookiecount"));
    
    for (int i = 0; i < count; i++) {
        Cookie cookie = new Cookie(props.getProperty("cookie_name" + i), props.getProperty("cookie_value" + i),
                props.getProperty("cookie_domain" + i), props.getProperty("cookie_path" + i),
                Boolean.parseBoolean(props.getProperty("cookie_secure" + i)),
                Long.parseLong(props.getProperty("cookie_expires" + i)));
        manager.add(cookie);
    }
    
    JMeterProperty cookieprop = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager");
    
    cookieprop.setObjectValue(manager);
    
    ctx.getCurrentSampler().setProperty(cookieprop);
    


  • 说明:

    第2点的代码从 HTTP Cookie Manager获取所有可用的cookie 并将它们存储到JMeter Properties,前缀为 cookie _

    The code at point 2 fetches all available cookies from HTTP Cookie Manager and stores them to JMeter Properties prefixed with cookie_

    第3点的代码读取所有属性前缀使用 cookie _ ,从中构造JMeter Cookie并将其添加到HTTP Cookie管理器。

    The code at point 3 reads all properties prefixed with cookie_, constructs JMeter Cookies from them and adds them to HTTP Cookie Manager.

    请参阅如何使用BeanShell 指南获取有关通过脚本扩展JMeter的更多信息。

    See How to use BeanShell guide for more information on extending JMeter via scripting.

    这篇关于jmeter - 第二次发布请求未使用在我的登录请求中创建的JSESSIONID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆