jmeter的请求参数为中文时,进行encode转码

JMeter中文参数编码解决方案
本文介绍了解决JMeter中中文参数编码问题的有效方法。通过使用org.apache.commons.httpclient.util.URIUtil包的encodeQuery方法,可以精准地对中文字符进行编码,同时避免对特殊符号的错误编码,确保接口参数正确传递。

在进行单接口调试的时候,遇到参数有中文的情况,我们会勾选 URL Encode 这个选项,如下图:

但是当用数据驱动的时候(参数从文件里获取),就无法这样操作,需要对带有中文参数进行encode

在网上查了一些资料,看了基本上有两种解决办法:

1.对jmeter源码进行二次开发,将编码格式改为UTF-8;

2.使用函数助手的__urlncode 函数进行编码:

试了一下第二种方法(第一种方法不敢试),发现确实有对中文的参数进行编码,但是对那些符号(=、%)也进行了编码,导致接口参数传的不对,这个方式还是有瑕疵(虽然可以避免);

于是就有了下面的方法:
使用org.apache.commons.httpclient.util.URIUtil 包里的encodeQuery方法,成功的、完美的将中文字符进行了编码,并且它优雅的过滤了那些非中文字符,具体使用方式:
添加一个beanshell 前置处理器,在里面输入如下的代码(前提是jar包要先放到本地ext目录下):

import org.apache.commons.httpclient.util.URIUtil;
t=new URIUtil();
	String url=vars.get("parameter");
	vars.put("parameter",t.encodeQuery(url));

至此,坑已解决。

POST http://221.178.78.110:29001/bit-id/3d-digital/avatar-api/v1/knowledge/file/upload POST data: --PnFqeU3sN_Qa_jtvSNrLg--79SC1AWwg9 Content-Disposition: form-data; name="knowledgeId" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1498870953575186442 --PnFqeU3sN_Qa_jtvSNrLg--79SC1AWwg9 Content-Disposition: form-data; name="srcFile"; filename="?????????? ???????? ?????????????.docx" Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document Content-Transfer-Encoding: binary <actual file content, not shown here> --PnFqeU3sN_Qa_jtvSNrLg--79SC1AWwg9-- Cookie Data: Admin-Mobile=18123669786; Admin-Token=Bearer%20eyJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoid2ViIiwiYWNjb3VudCI6IjE4MTIzNjY5Nzg2Iiwic3ViIjoiMTAwMDE4IiwiaXNzIjoiYXZhdGFyLXVzZXItc2VydmVyIiwiaWF0IjoxNzYxNTM0NTUyLCJleHAiOjE3NjIxMzkzNTIsIm5iZiI6MTc2MTUzNDU1Mn0.EmKE-RIXeXX2EN7bDcPlZLSElqK4c7NHd-39thNqoow日志java.io.FileNotFoundException: C:\Users\EDY\Desktop\shuzhirenxiangmu\wodezhishiku\jituanxinxi\jituanxinxi\���������� �ұ���Ӧ�� �й��ƶ�֣�س�ŵ��.docx (系统找不到指定的路径。) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:216) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:116) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl$ViewableFileBody.writeTo(HTTPHC4Impl.java:1513) at org.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:134) at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:157) at org.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:113) at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:156) at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:152) at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:238) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl$2.doSendRequest(HTTPHC4Impl.java:458) at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:935) at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:646) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:66) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1296) at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1285) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) at java.base/java.lang.Thread.run(Thread.java:833)
最新发布
10-28
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值