AH01387: Zlib: неверный заголовок
У меня есть следующий код:
Код: Выделить всё
public class HttpClientTester extends StandaloneApplication {
public static byte[] deflate(byte[] input) {
Deflater deflater = new Deflater();
deflater.setLevel(9);
deflater.setInput(input);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int compressedSize = deflater.deflate(buffer);
outputStream.write(buffer, 0, compressedSize);
}
return outputStream.toByteArray();
}
public static byte[] compress(byte[] input) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);
zipStream.write(input);
zipStream.flush();
zipStream.finish();
zipStream.close();
return byteStream.toByteArray();
}
public static String deflateReverse(byte[] input) throws DataFormatException, UnsupportedEncodingException {
Inflater inflater = new Inflater();
inflater.setInput(input);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int decompressedSize = inflater.inflate(buffer);
outputStream.write(buffer, 0, decompressedSize);
}
return new String(outputStream.toByteArray(), "UTF-8");
}
public static String decompress(byte[] input) throws IOException {
GZIPInputStream zippedInputStream = new GZIPInputStream(new ByteArrayInputStream(input));
return Tools.readStream(zippedInputStream, StandardCharsets.UTF_8);
}
@Override
public Void execute(StandaloneApplicationContext arg0) throws Exception {
Bundle sslProperties = new Bundle();
sslProperties.set(SSLProperties.SSL_VERSION, "TLSv1.2");
sslProperties.set(SSLProperties.SSL_TRUST_ALL_CERTS, true);
HTTPSHandler httpsHandler = new HTTPSHandler(null, arg0.getLoggingContext().getLoggingId(), sslProperties);
HttpClientV2 clientV2 = new HttpClientV2(null, arg0.getLoggingContext().getLoggingId(), arg0.getConfigurationContext().getParametersReader());
clientV2.setSSLHandler(httpsHandler);
HttpRequest httpRequest = HttpFactory.createHttpRequest(HttpConstants.HTTP_CONTENT_TYPE_TEXT_HTML);
httpRequest.setTransactionId(arg0.getLoggingContext().getLoggingId());
httpRequest.setHeaderField("Accept-encoding", "gzip");
httpRequest.setHeaderField("Content-encoding", "gzip");
httpRequest.setURL("https://myserver/myapp");
httpRequest.setMethod(HttpMethod.POST);
byte[] unzippedInputBody = "ERRORERROR WHILE PARSING XML REQUEST".getBytes("UTF-8");
byte[] zippedInputBody = compress(unzippedInputBody);
httpRequest.setBody(zippedInputBody);
httpRequest.setHeaderField("content-length", "" + zippedInputBody.length);
HttpResponse httpResponse = clientV2.doCommunication(httpRequest);
byte[] stream = httpResponse.getBodyStream();
String data = decompress(stream);
//@formatter:off
pause(
"Data length.........: " + stream.length + "\r\n"+
"Real data...........: " + data + "\r\n"+
"Response B64 data...: " + new Base64().encode(stream) + "\r\n"+
"Request B64 data....: " + new Base64().encode(zippedInputBody));
//@formatter:on
return null;
}
public static void main(String[] args) {
StandaloneApplicationContext.run(new HttpClientTester());
}
}
Код: Выделить всё
SetInputFilter DEFLATE
SetOutputFilter DEFLATE
В настоящее время несжатые данные представляют собой следующую строку:
Код: Выделить всё
ERRORERROR WHILE PARSING XML REQUEST
H4sIAAAAAAAAALPxTS0uTkxPdc0rS83JL7CzCaksSLVzDQryD7LRB7NtHPPycxNzKiGCuEenj6uCgGOQcGefu4KEb4+CkGugaGuwSE2+jCFNvpohgIA/IH0c2UAAAA=
Текущий XML получен, когда веб-приложение получило недопустимый запрос XML. Без сжатия GZIP по HTTP-запросу я могу получить его в сжатом виде и распаковать в своем Java-клиенте. Если я также попытаюсь сжать свой запрос, я получу 400 неверных запросов от HTTP-сервера без какой-либо возможности достичь моего веб-приложения.
Прикреплены обменянные сообщения, экспортированные журналами клиентов Java:
Сообщения
Подробнее здесь: https://stackoverflow.com/questions/786 ... ompression