jmeter响应数据Unicode编码转换为汉字(Convert the Unicode encoding of JMeter response data to Chinese characters)

每次用jmeter做接口测试时,响应信息中文总是显示Unicode编码格式,每次都要在网上寻找这一段转换的代码,但是我发现在网上找这段代码有点麻烦,像我这么懒的人觉得还是自己记录下来方便以后寻找。

1、添加后置处理器:BeanShell PostProcessor

2、粘贴以下代码:

//获取响应代码Unicode编码的 String s2=new String(prev.getResponseData(),”UTF-8″);//—————一下步骤为转码过程————— char aChar; int len= s2.length(); StringBuffer outBuffer=new StringBuffer(len); for(int x =0; x <len;){ aChar= s2.charAt(x++); if(aChar==’\\’){ aChar= s2.charAt(x++); if(aChar==’u’){ int value =0; for(int i=0;i<4;i++){ aChar= s2.charAt(x++); switch(aChar){ case’0′: case’1′: case’2′: case’3′: case’4′: case’5′: case’6′: case’7′: case’8′: case’9′: value=(value <<4)+aChar-‘0′; break; case’a’: case’b’: case’c’: case’d’: case’e’: case’f’: value=(value <<4)+10+aChar-‘a’; break; case’A’: case’B’: case’C’: case’D’: case’E’: case’F’: value=(value <<4)+10+aChar-‘A’; break; default: throw new IllegalArgumentException( “Malformed \\uxxxx encoding.”);}} outBuffer.append((char) value);}else{ if(aChar==’t’) aChar=’\t’; else if(aChar==’r’) aChar=’\r’; else if(aChar==’n’) aChar=’\n’; else if(aChar==’f’) aChar=’\f’; outBuffer.append(aChar);}}else outBuffer.append(aChar);}//—————–以上内容为转码过程—————————//将转成中文的响应结果在查看结果树中显示 prev.setResponseData(outBuffer.toString());

注意:如果还不能转换成中文,需要把jmeter中的配置文件jmeter.properties的配置项sampleresult.default.encoding 修改为utf-8,把“#”去掉;如不存在这一配置项就添加一行

sampleresult.default.encoding=utf-8

再重新启动jmeter、执行,就可以显示为中文了。

OK,大功告成,以后再也不用去网上大量搜索了。

————————

Every time JMeter is used for interface test, the response information in Chinese always displays Unicode coding format. Every time I have to look for this converted code on the Internet, but I find it a little troublesome to find this code on the Internet. People as lazy as me think it’s better to record it for future search.

1. Add postprocessor: BeanShell postprocessor

2. Paste the following code:

//获取响应代码Unicode编码的 String s2=new String(prev.getResponseData(),”UTF-8″);//—————一下步骤为转码过程————— char aChar; int len= s2.length(); StringBuffer outBuffer=new StringBuffer(len); for(int x =0; x <len;){ aChar= s2.charAt(x++); if(aChar==’\\’){ aChar= s2.charAt(x++); if(aChar==’u’){ int value =0; for(int i=0;i<4;i++){ aChar= s2.charAt(x++); switch(aChar){ case’0′: case’1′: case’2′: case’3′: case’4′: case’5′: case’6′: case’7′: case’8′: case’9′: value=(value <<4)+aChar-‘0′; break; case’a’: case’b’: case’c’: case’d’: case’e’: case’f’: value=(value <<4)+10+aChar-‘a’; break; case’A’: case’B’: case’C’: case’D’: case’E’: case’F’: value=(value <<4)+10+aChar-‘A’; break; default: throw new IllegalArgumentException( “Malformed \\uxxxx encoding.”);}} outBuffer.append((char) value);}else{ if(aChar==’t’) aChar=’\t’; else if(aChar==’r’) aChar=’\r’; else if(aChar==’n’) aChar=’\n’; else if(aChar==’f’) aChar=’\f’; outBuffer.append(aChar);}}else outBuffer.append(aChar);}//—————–以上内容为转码过程—————————//将转成中文的响应结果在查看结果树中显示 prev.setResponseData(outBuffer.toString());

Note: if it cannot be converted to Chinese, the configuration file in JMeter needs to be JMeter Properties configuration item sampleresult default. Encoding is changed to UTF-8, and “#” is removed; If this configuration item does not exist, add a line

sampleresult.default.encoding=utf-8

Restart JMeter and execute again, and it can be displayed in Chinese.

OK, it’s done. You don’t have to search a lot on the Internet in the future.