网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

nGrinder中快速编写groovy脚本04-发送POST请求-创新互联

POST请求分类:
1、根据是否修改代码,分为两种方式:
一种是在UI界面添加后自动生成脚本,一种是直接在脚本中添加
2、根据请求参数的不同,主要可以分为两种:
param为key value格式
body为json格式
一、通过UI方式发送POST请求–key/value参数
通过 UI 设置:脚本 -> 新建脚本 -> 显示高级配置
当选择了请求方法为POST后,在高级配置中默认会在headers中显示Content-Type为x-www-form-urlencoded,同时,添加key/value格式的params:
nGrinder中快速编写groovy脚本04-发送POST请求
生成代码如下(由于篇幅限制,去掉import部分):
@RunWith(GrinderRunner)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestRunner {

public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []

@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "www.baidu.com")
request = new HTTPRequest()
// Set header datas
List headerList = new ArrayList()
headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
headerList.add(new NVPair("Connection", "keep-alive"))
headers = headerList.toArray()
// Set param datas
List paramList = new ArrayList()
paramList.add(new NVPair("name", "jing"))
paramList.add(new NVPair("age", "18"))
paramList.add(new NVPair("desc", "beauty"))
params = paramList.toArray()
// Set cookie datas
List cookieList = new ArrayList()
cookieList.add(new Cookie("token", "xxxxxxxx", "www.baidu.com", "", new Date(32503647599000L), false))
cookies = cookieList.toArray()
grinder.logger.info("before process.");
}

@BeforeThread
public void beforeThread() {
test.record(this, "test")
grinder.statistics.delayReports=true;
grinder.logger.info("before thread.");
}

@Before
public void before() {
request.setHeaders(headers)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before thread. init headers and cookies");
}

@Test
public void test(){
HTTPResponse result = request.POST("https://www.baidu.com", params)

assertThat(result.statusCode, is(200))
assertThat(result.getText(), containsString("jing"))
}
}

从以上代码可以看出,这种方式跟之前使用GET方式添加参数的效果一样,都是把参数添加在@BeforeProcess,不同的只是在@Test中使用的是request.POST方法。

二、通过UI方式发送POST请求–json
通过 UI 设置:脚本 -> 新建脚本 -> 显示高级配置
当选择了请求方法为POST后,在高级配置中的headers中选择Content-Type为application/json,同时,添加json字符串:
nGrinder中快速编写groovy脚本04-发送POST请求
生成代码如下(由于篇幅限制,去掉import部分):
@RunWith(GrinderRunner)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestRunner {

public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static String body = "{"name":"jing","comment":"hello"}"
public static Cookie[] cookies = []

@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "www.baidu.com")
request = new HTTPRequest()
// Set header datas
List headerList = new ArrayList()
headerList.add(new NVPair("Content-Type", "application/json"))
headers = headerList.toArray()
grinder.logger.info("before process.");
}

@BeforeThread
public void beforeThread() {
test.record(this, "test")
grinder.statistics.delayReports=true;
grinder.logger.info("before thread.");
}

@Before
public void before() {
request.setHeaders(headers)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before thread. init headers and cookies");
}

@Test
public void test(){
HTTPResponse result = request.POST("https://www.baidu.com", body.getBytes())

assertThat(result.statusCode, is(200))
}
}

从以上代码可以看出,这种方式是在静态变量中定义了body的内容,在@BeforeProcess中添加json请求头,并在@Test中的request.POST方法中加入了body参数。
关键代码如下:

public static String body = "{"name":"jing","comment":"hello"}"

headerList.add(new NVPair("Content-Type", "application/json"))

HTTPResponse result = request.POST("https://www.baidu.com", body.getBytes())

三、直接在脚本中为POST请求添加json格式的body
使用UI方式添加的json字符串默认是在创建静态变量body的同时添加的;
直接修改脚本的话,就比较灵活,可以在类的任意位置添加,然后在POST请求中调用
(但是要注意变量作用域的问题)
// 定义json字符串
String jsonStr = '{"name": "jing"}';

//在@Test的POST方法中使用json字符串,同时添加header
request.POST("https://www.baidu.com", jsonStr.getBytes(), [
    new NVPair("Content-Type", "application/json"
] as NVPair[])

其中,需要注意的是:

POST(java.lang.String uri, byte[] data)
此方法中接收body参数时需要把字符串转成字节数组。

站在用户的角度思考问题,与客户深入沟通,找到芒市网站设计与芒市网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站制作、网站设计、企业官网、英文网站、手机端网站、网站推广、主机域名雅安服务器托管、企业邮箱。业务覆盖芒市地区。
网页名称:nGrinder中快速编写groovy脚本04-发送POST请求-创新互联
文章位置:http://bjjierui.cn/article/dpjdgj.html

其他资讯