jmeter - 第二个帖子请求没有使用在我的登录帖子请求中创建的 JSESSIONID [英] jmeter - second post request is not using the JSESSIONID created on my log in post request

查看:17
本文介绍了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- 使用 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设置-Cookie:JSESSIONID=F16BF82FD28A84F6E28DDE30DECDC48C;路径=/;安全的;仅Http位置: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 字段:内容类型:数据编码:空

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}}

[没有饼干]

请求头:连接:保持连接内容类型:应用程序/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 请求,但它们正在工作.他们有跟随重定向,它首先转到 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.

您只需要在 jmeter.properties 文件中设置 CookieManager.save.cookies=true 属性,或者将其作为参数添加到 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 管理器作为登录请求的子项添加时才可重现.如果是您的情况 - 将其向上移动 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 的身份验证存在问题,服务器宁愿使用 401403 之类的内容进行响应.

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,它仍然可以通过即 Beanshell 来完成

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

您需要执行以下操作:

  1. 如果您要在不同线程组之间共享 cookie 或出于任何其他原因需要它们作为 JMeter 变量,请在 jmeter.properties<中设置 CookieManager.save.cookies=true 属性/code> 文件或在 JMeter 启动期间将其指定为 jmeter -JCookieManager.save.cookies=true
  2. 使用以下代码将 Beanshell 后处理器添加到您的登录请求:

  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 和将它们存储到以 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天全站免登陆