From 3ca3618c3c189daf65ea5da7b7a3af7dc24d0acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E5=AE=87=E7=90=A6?= <465615774@qq.com> Date: Fri, 26 Jun 2020 16:57:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=84=E4=B8=AA=E5=BD=A2?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E5=93=8D=E5=BA=94=E6=95=B0=E6=8D=AE=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E8=BF=94=E5=9B=9E=EF=BC=8C=E5=B9=B6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9html=E8=BF=94=E5=9B=9E=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ++ .../auxiliary/tool/http/EasyResponse.java | 103 ++++++++++++++++-- .../auxiliary/tool/http/EasyHttpTest.java | 1 - .../auxiliary/tool/http/EasyResponseTest.java | 83 ++++++++++++++ 4 files changed, 183 insertions(+), 11 deletions(-) create mode 100644 src/test/java/pres/auxiliary/tool/http/EasyResponseTest.java diff --git a/pom.xml b/pom.xml index e8b8649..2a2ce31 100644 --- a/pom.xml +++ b/pom.xml @@ -109,5 +109,12 @@ 4.5.12 + + + org.jsoup + jsoup + 1.13.1 + + diff --git a/src/main/java/pres/auxiliary/tool/http/EasyResponse.java b/src/main/java/pres/auxiliary/tool/http/EasyResponse.java index 007d89e..960ac40 100644 --- a/src/main/java/pres/auxiliary/tool/http/EasyResponse.java +++ b/src/main/java/pres/auxiliary/tool/http/EasyResponse.java @@ -1,11 +1,21 @@ package pres.auxiliary.tool.http; -import org.dom4j.Document; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; + import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.XMLWriter; +import org.jsoup.Jsoup; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; public class EasyResponse { /** @@ -19,9 +29,14 @@ public class EasyResponse { private JSONObject responseJson = null; /** - * 以HTML的形式或XML的形式存储响应数据,通过DocumentHelper.parseText(String)方法可以转换 + * 以XML的形式存储响应数据,通过DocumentHelper.parseText(String)方法可以转换 */ - private Document responseDom = null; + private org.dom4j.Document responseXmlDom = null; + + /** + * 以HTML的形式存储响应数据,通过Jsoup.parse(String)方法可以转换 + */ + private org.jsoup.nodes.Document responseHtmlDom = null; /** * 存储响应数据的类型 @@ -47,15 +62,25 @@ public class EasyResponse { responseJson = JSONObject.parseObject(responseText); responseType = ResponseType.JSON; } catch (JSONException jsonException) { + //设置responseJson为null responseJson = null; - //转换为Document格式,若不能转换,则dom为null + //先通过dom4j的格式对数据进行转换,若不能转换,则表示其是文本形式 try { - responseDom = DocumentHelper.parseText(responseText); - //根据xml格式的特点进行判断,若响应数据的第一位为",则表示其相应参数为html形式,则以html形式进行转换 + if (responseText.indexOf("") == 0) { + //存储html形式 + responseType = ResponseType.HTML; + //设置responseXmlDom为null,保证返回的数据正确 + responseXmlDom = null; + //将文本转换为HTML的形式 + responseHtmlDom = Jsoup.parse(responseText); + } else { + responseType = ResponseType.XML; + } } catch (DocumentException domExcepttion) { - responseDom = null; + responseXmlDom = null; //若响应数据无法转换成json或dom,则存储为纯文本形式 responseType = ResponseType.TEXT; } @@ -71,6 +96,56 @@ public class EasyResponse { return responseText; } + /** + * 以格式化的形式输出响应数据。 + * @return 格式化后的响应数据 + */ + public String getFormatResponseText() { + //根据responseType存储的形式对格式进行转换 + switch (responseType) { + case HTML: + return responseHtmlDom.html(); + case JSON: + return JSON.toJSONString(responseJson, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); + case XML: + OutputFormat format = OutputFormat.createPrettyPrint(); + format.setEncoding(responseXmlDom.getXMLEncoding()); + StringWriter stringWriter = new StringWriter(); + XMLWriter writer = new XMLWriter(stringWriter, format); + try { + writer.write(responseXmlDom); + writer.close(); + } catch (IOException e) { + } + return stringWriter.toString(); + case EMPTY: + case TEXT: + return responseText; + default: + return ""; + } + } + + /** + * 用于将响应数据写入到文件中 + * @param outputFile 需要输出的文件 + * @param isFormat 是否格式化输出 + * @return 写入的文件 + * @throws IOException 写入文件有误时抛出的异常 + */ + public File responseToFile(File outputFile, boolean isFormat) throws IOException { + //创建文件夹 + outputFile.getParentFile().mkdirs(); + //获取响应数据 + String responseText = isFormat ? getFormatResponseText() : this.responseText; + + BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile)); + bw.write(responseText); + bw.close(); + + return outputFile; + } + /** * 用于以{@link JSONObject}类的形式返回响应数据,若响应数据不是json格式时,则返回null * @return {@link JSONObject}类形式的响应数据 @@ -79,12 +154,20 @@ public class EasyResponse { return responseJson; } + /** + * 用于以{@link org.jsoup.nodes.Document}类的形式返回响应数据,若响应数据不是html格式时,则返回null + * @return {@link org.jsoup.nodes.Document}类形式的响应数据 + */ + public org.jsoup.nodes.Document getHtmlDocument() { + return responseHtmlDom; + } + /** * 用于以{@link Document}类的形式返回响应数据,若响应数据不是html或xml格式时,则返回null * @return {@link Document}类形式的响应数据 */ - public Document getDocument() { - return responseDom; + public org.dom4j.Document getXmlDocument() { + return responseXmlDom; } /** diff --git a/src/test/java/pres/auxiliary/tool/http/EasyHttpTest.java b/src/test/java/pres/auxiliary/tool/http/EasyHttpTest.java index 9e01fb6..9afd387 100644 --- a/src/test/java/pres/auxiliary/tool/http/EasyHttpTest.java +++ b/src/test/java/pres/auxiliary/tool/http/EasyHttpTest.java @@ -1,7 +1,6 @@ package pres.auxiliary.tool.http; import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class EasyHttpTest { diff --git a/src/test/java/pres/auxiliary/tool/http/EasyResponseTest.java b/src/test/java/pres/auxiliary/tool/http/EasyResponseTest.java new file mode 100644 index 0000000..d6f83ed --- /dev/null +++ b/src/test/java/pres/auxiliary/tool/http/EasyResponseTest.java @@ -0,0 +1,83 @@ +package pres.auxiliary.tool.http; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; + +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.alibaba.fastjson.JSONObject; + +public class EasyResponseTest { + EasyResponse re; + + @BeforeMethod + public void showMethodName(Method m) { + System.out.println("---------------------------"); + System.out.println("运行" + m.getName() + "方法:"); + } + + /** + * 以格式化的形式输出响应参数,测试{@link EasyResponse#getFormatResponseText()}方法 + */ + @AfterMethod + public void showFormatResponse() { + System.out.println("***************************"); + System.out.println(re.getFormatResponseText()); + System.out.println("---------------------------"); + } + + /** + * 测试{@link EasyResponse#getResponseText()}方法 + */ + @Test + public void getResponseTextTest() { + re = new EasyResponse("success"); + System.out.println(re.getResponseText()); + } + + /** + * 测试{@link EasyResponse#getResponseText()}方法 + */ + @Test + public void getResponseJsonTest() { + re = new EasyResponse("{\"result\":\"false\",\"code\":\"29\",\"status\":203,\"message\":\"Invalid Api Key\",\"detail_message\":\"由服务器端分配给手机端\",\"result_data\":{}}"); + JSONObject json = re.getResponseJson(); + System.out.println(json.get("result")); + } + + /** + * 测试{@link EasyResponse#getResponseText()}方法 + */ + @Test + public void getHtmlDocumentTest() { + re = new EasyResponse("
"); + org.jsoup.nodes.Document dom = re.getHtmlDocument(); + System.out.println(dom.getElementById("div").tagName()); + } + + /** + * 测试{@link EasyResponse#getResponseText()}方法 + */ + @Test + public void getXmlDocumentTest() { + re = new EasyResponse("//XXX模板控件1[@X='${name}']/div/div[@${att}='${id}']/inputhttp body ${tagName}"); + org.dom4j.Document dom = re.getXmlDocument(); + System.out.println(dom.getRootElement().getName()); + } + + /** + * 测试{@link EasyResponse#responseToFile()}方法 + * @throws IOException + */ + @Test + public void responseToFileTest() throws IOException { + re = new EasyResponse("//XXX模板控件1[@X='${name}']/div/div[@${att}='${id}']/inputhttp body ${tagName}"); + File file = new File("D:\\8.test\\EasyResponse\\EasyResponseTest.txt"); + re.responseToFile(file, true); + + java.awt.Desktop.getDesktop().open(file.getParentFile()); + } +}