From c52820b1aed944c58b9a5a055a46c79fc556c4cb Mon Sep 17 00:00:00 2001 From: hmm Date: Mon, 1 Sep 2014 10:42:05 +0800 Subject: [PATCH] encode response with gzip --- .../RecordScriptTestCase/baidu.html | 89 ++++++++++++++++++- .../recorder/httpcapture/RequestHandler.java | 2 +- .../generator/AbstractCodeGenerator.java | 4 +- .../generator/Bench4qCodeGenerator.java | 7 +- .../httpcapture/generator/GzipDecoder.java | 2 +- .../httpcapture/generator/ResponseParser.java | 13 +++ .../bench4q/recorder/Test_RequestHandler.java | 7 +- 7 files changed, 113 insertions(+), 11 deletions(-) diff --git a/Bench4Q-Recorder/RecordScriptTestCase/baidu.html b/Bench4Q-Recorder/RecordScriptTestCase/baidu.html index 1f1ae41a..5f21f00a 100644 --- a/Bench4Q-Recorder/RecordScriptTestCase/baidu.html +++ b/Bench4Q-Recorder/RecordScriptTestCase/baidu.html @@ -1 +1,88 @@ -百度一下,你就知道
百度一下,你就知道

显示卡片宝箱换肤消息 设为首页 wonanguo11

加载中,精彩马上呈现
©2014 Baidu 使用百度前必读 京ICP证030173号
0); doEndTransaction(); @@ -444,7 +444,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator, * @param rootUrl * @return new response body */ - public abstract byte[] doParseHtmlContent(String responseBody, String rootUrl); + public abstract String doParseHtmlContent(String responseBody, String rootUrl); public abstract void doHeaders(HeaderValue[] paramArrayOfHeaderValue); diff --git a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/Bench4qCodeGenerator.java b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/Bench4qCodeGenerator.java index 2499dc9f..2dfed0a7 100644 --- a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/Bench4qCodeGenerator.java +++ b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/Bench4qCodeGenerator.java @@ -2,6 +2,7 @@ package org.bench4q.recorder.httpcapture.generator; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; @@ -200,11 +201,11 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator { } @Override - public byte[] doParseHtmlContent(String responseBody, String rootUrl) { + public String doParseHtmlContent(String responseBody, String rootUrl) { int htmlStart = responseBody.indexOf(""); int bodyEnd = responseBody.indexOf(""); @@ -216,7 +217,7 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator { responseBody += ""; htmlEnd = responseBody.indexOf(""); } - return parseDocument(rootUrl, responseBody, htmlStart, htmlEnd).getBytes(); + return parseDocument(rootUrl, responseBody, htmlStart, htmlEnd); } private String parseDocument(String rootUrl, String responseContent, diff --git a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/GzipDecoder.java b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/GzipDecoder.java index e83a7231..adb7e014 100644 --- a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/GzipDecoder.java +++ b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/GzipDecoder.java @@ -6,6 +6,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; public class GzipDecoder extends ContentDecoder { @@ -25,7 +26,6 @@ public class GzipDecoder extends ContentDecoder { return null; } } - public String decodeContent(InputStream inputStream, Charset charset){ InputStream is = inputStream; GZIPInputStream gzin; diff --git a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java index 8a972ea8..856a8109 100644 --- a/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java +++ b/Bench4Q-Recorder/src/main/java/org/bench4q/recorder/httpcapture/generator/ResponseParser.java @@ -183,4 +183,17 @@ public class ResponseParser { } this.setResponseBody(new String(contentBodyAfterDecoded, charset)); } + + public byte[] encodeCoent(String responseBody){ + ContentEncoder contentEncoder = ContentEncoder.createEncoder(this + .getResponseHeader().getContentEncoding()); + Charset charset = null; + try { + charset = Charset.forName(this.getResponseHeader().getCharset()); + } catch (Exception e) { + charset = Charset.forName("utf-8"); + logger.error(e, e); + } + return contentEncoder.encoderContent(responseBody.getBytes(charset)); + } } diff --git a/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java b/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java index eaf18c0f..6a5ea156 100644 --- a/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java +++ b/Bench4Q-Recorder/src/test/java/org/bench4q/recorder/Test_RequestHandler.java @@ -1,6 +1,7 @@ package org.bench4q.recorder; import java.io.IOException; +import java.nio.charset.Charset; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; @@ -52,12 +53,12 @@ public class Test_RequestHandler { get.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); get.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8"); get.addRequestHeader("Cache-Control", "max-age=0"); -// get.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch"); -// get.addRequestHeader("Cookie","__zpspc=188.1.1409195668.1409195668.1%234%7C%7C%7C%7C%7C; BDUSS=E45eXUtamxkY2JUbS12ZjFUSmJPbWdmUU13Ri1ZVHlzWWx2S1B3YXRxRVNUU1pVQVFBQUFBJCQAAAAAAAAAAAEAAAAOfOsxd29uYW5ndW8xMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABLA~lMSwP5Tc; cflag=65535%3A1; BD_UPN=123143; BD_HOME=1; BAIDUID=12B697CB05FD662D2CA4416FF118FCD8:FG=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BD_CK_SAM=1; H_PS_PSSID=8307_5228_1466_7800_8235_8488_8057_6506_6017_8251_7607_7799_8483_8457_8167_8509_8435_8382_8114; BD_HOME=1"); + get.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch"); + get.addRequestHeader("Cookie","__zpspc=188.1.1409195668.1409195668.1%234%7C%7C%7C%7C%7C; BDUSS=E45eXUtamxkY2JUbS12ZjFUSmJPbWdmUU13Ri1ZVHlzWWx2S1B3YXRxRVNUU1pVQVFBQUFBJCQAAAAAAAAAAAEAAAAOfOsxd29uYW5ndW8xMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABLA~lMSwP5Tc; cflag=65535%3A1; BD_UPN=123143; BD_HOME=1; BAIDUID=12B697CB05FD662D2CA4416FF118FCD8:FG=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BD_CK_SAM=1; H_PS_PSSID=8307_5228_1466_7800_8235_8488_8057_6506_6017_8251_7607_7799_8483_8457_8167_8509_8435_8382_8114; BD_HOME=1"); int statusCode = this.httpClient.executeMethod(get); System.out.println(statusCode); - System.out.println(get.getResponseBodyAsString()); + System.out.println(new String(get.getResponseBody(),Charset.forName("utf-8"))); } catch (URIException e) { // TODO Auto-generated catch block e.printStackTrace();