The content-type of the data returned by the request interface is br parsing

Brotli is a brand new data format that can provide a 20-26% higher compression ratio than Zopfli.

Brotli was originally released in 2015 for offline compression of web fonts. Google software engineers released an enhanced version of Brotli with general lossless data compression in September 2015, with a particular focus on HTTP compression.

The content encoding type that uses Brotli for stream compression has been proposed to use "br".

Use httpClient to make requests

Dependencies to be introduced:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.18</version>
</dependency>

<dependency>
    <groupId>org.brotli</groupId>
    <artifactId>dec</artifactId>
    <version>0.1.2</version>
</dependency>

Parsing: By parsing the returned data, it is found that the Content-Type is br. If the returned stream is directly converted to a string, it is garbled. You need to use BrotliCompressorInputStream to receive it, and then convert it to InputStream to parse it as a string.

 1 private static String deCodeStream(InputStream stream, String codeType) {
 2     if (codeType.contains("br")) {
 3         try {
 4             BrotliCompressorInputStream brStream = new BrotliCompressorInputStream(stream);
 5             BufferedInputStream inputStream = new BufferedInputStream(brStream);
 6             ByteArrayOutputStream result = new ByteArrayOutputStream();
 7             byte[] buffer = new byte[1024];
 8             int length;
 9             while ((length = inputStream.read(buffer)) != -1) {
10                 result.write(buffer, 0, length);
11             }
12             String str = result.toString(StandardCharsets.UTF_8.name());
13             System.out.println(str);
14             brStream.close();
15             inputStream.close();
16             result.close();
17             return str;
18         } catch (IOException e) {
19             System.out.println(e.getMessage());
20         }
21     }
22     return "";
23 }

 

Guess you like

Origin www.cnblogs.com/oumae/p/12681760.html