Solve jmeter request parameter Chinese garbled

Today, when I used jmeter to write a script, I found that the Chinese parameter value of the request post request in the view result tree was garbled, so I recorded the solution process.

write picture description here

The solution process is as follows:
1. Modify the local configuration file

Because the data here has not been sent out, it must be caused by the inconsistency between the encoding of this variable and some encodings inside jmeter. Then, try to modify the configuration items of jmeter.properties:

sampleresult.default.encoding=utf-8

After restarting jmeter, it is still garbled.

2. Modify the message header and request body encoding

write picture description here
At the same time, the encoding of the message header and request body is changed to utf-8, but the result still does not take effect.

3. View jmeter source code

Because it is the content of the request body, all first check it in the following package:

write picture description here

    public static String decodeQuery(String query) {
        if (query != null && query.length() > 0) {
            try {
                query = URLDecoder.decode(query, "ISO-8859-1"); // 此处的字符编码为ISO-8859-1 ,将其改为utf-8
                return query;
            } catch (IllegalArgumentException arg1) {
                log.warn(
                        "Error decoding query, maybe your request parameters should be encoded:"
                                + query, arg1);
                return null;
            } catch (UnsupportedEncodingException arg2) {
                log.warn(
                        "Error decoding query, maybe your request parameters should be encoded:"
                                + query, arg2);
                return null;
            }
        } else {
            return null;
        }
    }

Eclipse installs the decompiler plugin (Help - Eclipse Marketplace enter Decompiler to search for and install this plugin - the result after installation and restart)
write picture description here

4 Decompile RequestViewHTTP.class, change ISO-8859-1 to UTF-8, then compile and replace the original jar with the latest class package.

Restart jmeter, and when requesting again, the request parameters are as follows:
write picture description here

Let's talk about the HTTP protocol recording Chinese problem.
During the HTTP protocol test, we often use the HTTP proxy server provided by JMeter to record the test script. A typical recording plan is as follows:

write picture description here

Since Chinese is entered in the showName item in the browser, Chinese garbled characters appear in the POST form of the HTTPSampler generated by the recording.
We can find the following code in the org.apache.jmeter.protocol.http.proxy.DefaultSamplerCreator.class source file in the org.apache.jmeter.protocol.http.proxy package:

            if (!StringUtils.isEmpty(contentEncoding)) {
                postData = new String(request.getRawPostData(), contentEncoding);
            } else {
                postData = new String(request.getRawPostData(), "ISO-8859-1"); 
                //将ISO-8859-1编码改为UTF-8
            }

It will be found that the reason for Chinese garbled characters is that the information cannot be read from Content encoding, and the default ISO-8859-1 encoding is used. Therefore, we only need to specify our Chinese encoding to solve the above problems. The easiest solution is to comment Remove the above code and set a global default Chinese encoding, as follows:
postData = new String(request.getRawPostData(), "utf-8");

Then replace the newly compiled DefaultSamplerCreator.class with the original class file in the ApacheJMeter_http.jar file under E:\jmeter\apache-jmeter-2.11\lib\ext.
Record again, you can see that Chinese can be displayed normally.

Let's talk about the Content encoding option in the Jmeter HTTP request.
write picture description here
We know that in the existing JMeter version, HTTPSampler can mainly choose Java, HttpClient3.1 and HttpClient4 to set the real implementation method of finally simulating the interaction between the HTTP protocol and the Web server. Here I use the Java implementation method as a reference, check the source file PostWriter.java from the JMeter source package org.apache.jmeter.protocol.http.sampler, and find this line:

write picture description here

write picture description here

It can be seen that if the Content encoding item is not set, JMeter also defaults to ISO-8859-1 encoding, a once and for all method (this is just an example for HTTPSampler, but it can be extended to all Samplers that require Chinese character encoding according to this idea ), we only need to change the default encoding to utf-8, gb18030, etc. according to the Chinese encoding requirements, and then we can POST the correct Chinese to the Web server. Refer to the following modifications:


String contentEncoding = sampler.getContentEncoding();
if (contentEncoding == null || contentEncoding.length() == 0) {
     contentEncoding = "UTF-8";
   }

Then replace the newly compiled PostWriter.class with the original class file in the ApacheJMeter_http.jar file under E:\jmeter\apache-jmeter-2.11\lib\ext. In
this way, the Content encoding is not written as utf-8, and the default is also in accordance with UTF- 8 processed.

Or use a simple method, which is to set the Content encoding item to complete the Chinese encoding, as shown in the following figure:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325280094&siteId=291194637