添加简易接口调用类方法
This commit is contained in:
parent
c9179f4caa
commit
d2adeba8aa
|
@ -3,8 +3,6 @@ package pres.auxiliary.tool.http;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p><b>文件名:</b>EasyHttp.java</p>
|
* <p><b>文件名:</b>EasyHttp.java</p>
|
||||||
* <p><b>用途:</b>
|
* <p><b>用途:</b>
|
||||||
|
@ -18,9 +16,9 @@ import com.alibaba.fastjson.JSONObject;
|
||||||
*/
|
*/
|
||||||
public class EasyHttp implements Cloneable {
|
public class EasyHttp implements Cloneable {
|
||||||
/**
|
/**
|
||||||
* 存储指定的请求方式
|
* 存储指定的请求方式,默认get请求
|
||||||
*/
|
*/
|
||||||
private RequestType requestType;
|
private RequestType requestType = RequestType.GET;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储接口请求地址,及相应拼接的参数,用于最终请求
|
* 存储接口请求地址,及相应拼接的参数,用于最终请求
|
||||||
|
@ -34,11 +32,11 @@ public class EasyHttp implements Cloneable {
|
||||||
/**
|
/**
|
||||||
* 存储主机,IP或者域名
|
* 存储主机,IP或者域名
|
||||||
*/
|
*/
|
||||||
private String host = "";
|
private String host = "127.0.0.1";
|
||||||
/**
|
/**
|
||||||
* 存储端口
|
* 存储端口
|
||||||
*/
|
*/
|
||||||
private String port = "";
|
private String port = "80";
|
||||||
/**
|
/**
|
||||||
* 存储接口路径
|
* 存储接口路径
|
||||||
*/
|
*/
|
||||||
|
@ -50,10 +48,15 @@ public class EasyHttp implements Cloneable {
|
||||||
private HashMap<String, String> parameterMap = new HashMap<>(16);
|
private HashMap<String, String> parameterMap = new HashMap<>(16);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于存储请求体
|
* 存储请求体
|
||||||
*/
|
*/
|
||||||
private String body = "";
|
private String body = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储字体编码
|
||||||
|
*/
|
||||||
|
private String encoding = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于存储请求头
|
* 用于存储请求头
|
||||||
*/
|
*/
|
||||||
|
@ -110,14 +113,14 @@ public class EasyHttp implements Cloneable {
|
||||||
host(inter.substring(0, index));
|
host(inter.substring(0, index));
|
||||||
} else {
|
} else {
|
||||||
host(inter);
|
host(inter);
|
||||||
//若其中未包含冒号,则表示端口为80
|
|
||||||
port("80");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//裁剪参数
|
//裁剪参数
|
||||||
Arrays.stream(param.split("&")).forEach(parameterText -> {
|
if (!param.isEmpty()) {
|
||||||
putParameter(parameterText);
|
Arrays.stream(param.split("&")).forEach(parameterText -> {
|
||||||
});
|
putParameter(parameterText);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -206,6 +209,21 @@ public class EasyHttp implements Cloneable {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于根据请求头枚举类{@link HeadType}设置请求头,若请求头名存在时,则覆盖上一次设置的值
|
||||||
|
* @param headType {@link HeadType}枚举类
|
||||||
|
* @return 类本身
|
||||||
|
*/
|
||||||
|
public EasyHttp putHead(HeadType headType) {
|
||||||
|
putHead(headType.getKey(), headType.getValue());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyHttp encoding(String encoding) {
|
||||||
|
this.encoding = encoding;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回设置的url
|
* 返回设置的url
|
||||||
* @return url
|
* @return url
|
||||||
|
@ -230,12 +248,12 @@ public class EasyHttp implements Cloneable {
|
||||||
}
|
}
|
||||||
|
|
||||||
//判断请求方式是否为get请求,若为get请求时,若设置了body,则将body拼接至末尾
|
//判断请求方式是否为get请求,若为get请求时,若设置了body,则将body拼接至末尾
|
||||||
if (requestType == RequestType.GET) {
|
if (requestType == RequestType.GET && !body.isEmpty()) {
|
||||||
//根据parameterMap是否为空判断,应拼接何种连接符,若parameterMap存在参数,则拼接"&"符号
|
//根据parameterMap是否为空判断,应拼接何种连接符,若parameterMap存在参数,则拼接"&"符号
|
||||||
url.append(parameterMap.isEmpty() ? "?" : "&");
|
url.append(parameterMap.isEmpty() ? "?" : "&");
|
||||||
}
|
}
|
||||||
|
|
||||||
//返回参数,并将空格转换为20%
|
//返回参数,并将空格转换为%20
|
||||||
return url.toString().replaceAll(" ", "20%");
|
return url.toString().replaceAll(" ", "%20");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,131 @@
|
||||||
|
package pres.auxiliary.tool.http;
|
||||||
|
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentException;
|
||||||
|
import org.dom4j.DocumentHelper;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONException;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
|
public class EasyResponse {
|
||||||
|
/**
|
||||||
|
* 存储接口响应数据
|
||||||
|
*/
|
||||||
|
private String responseText = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以Json类的形式存储响应数据
|
||||||
|
*/
|
||||||
|
private JSONObject responseJson = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以HTML的形式或XML的形式存储响应数据,通过DocumentHelper.parseText(String)方法可以转换
|
||||||
|
*/
|
||||||
|
private Document responseDom = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储响应数据的类型
|
||||||
|
*/
|
||||||
|
private ResponseType responseType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造类,传入接口的响应数据,并根据响应参数的类型对响应参数进行转换,指定响应参数的类型
|
||||||
|
* @param responseText 接口响应数据
|
||||||
|
*/
|
||||||
|
public EasyResponse(String responseText) {
|
||||||
|
//判断响应数据是否为空,若为空,则无需做任何处理
|
||||||
|
if (responseText.isEmpty()) {
|
||||||
|
responseType = ResponseType.EMPTY;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//字符串形式存储
|
||||||
|
this.responseText = responseText;
|
||||||
|
|
||||||
|
//转换为JSONObject格式,若不能转换,则responseJson为null
|
||||||
|
try {
|
||||||
|
responseJson = JSONObject.parseObject(responseText);
|
||||||
|
responseType = ResponseType.JSON;
|
||||||
|
} catch (JSONException jsonException) {
|
||||||
|
responseJson = null;
|
||||||
|
|
||||||
|
//转换为Document格式,若不能转换,则dom为null
|
||||||
|
try {
|
||||||
|
responseDom = DocumentHelper.parseText(responseText);
|
||||||
|
//根据xml格式的特点进行判断,若响应数据的第一位为"<?xml",则表示该文本是xml格式
|
||||||
|
responseType = responseText.indexOf("<?xml") == 0 ? ResponseType.XML : ResponseType.HTML;
|
||||||
|
} catch (DocumentException domExcepttion) {
|
||||||
|
responseDom = null;
|
||||||
|
//若响应数据无法转换成json或dom,则存储为纯文本形式
|
||||||
|
responseType = ResponseType.TEXT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于以文本形式返回响应数据
|
||||||
|
* @return 响应数据
|
||||||
|
*/
|
||||||
|
|
||||||
|
public String getResponseText() {
|
||||||
|
return responseText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于以{@link JSONObject}类的形式返回响应数据,若响应数据不是json格式时,则返回null
|
||||||
|
* @return {@link JSONObject}类形式的响应数据
|
||||||
|
*/
|
||||||
|
public JSONObject getResponseJson() {
|
||||||
|
return responseJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于以{@link Document}类的形式返回响应数据,若响应数据不是html或xml格式时,则返回null
|
||||||
|
* @return {@link Document}类形式的响应数据
|
||||||
|
*/
|
||||||
|
public Document getDocument() {
|
||||||
|
return responseDom;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于返回响应数据的类型
|
||||||
|
* @return 响应数据类型
|
||||||
|
*/
|
||||||
|
public ResponseType getResponseType() {
|
||||||
|
return responseType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p><b>文件名:</b>EasyResponse.java</p>
|
||||||
|
* <p><b>用途:</b>
|
||||||
|
* 定义响应数据的所有格式
|
||||||
|
* </p>
|
||||||
|
* <p><b>编码时间:</b>2020年6月24日上午8:18:03</p>
|
||||||
|
* <p><b>修改时间:</b>2020年6月24日上午8:18:03</p>
|
||||||
|
* @author 彭宇琦
|
||||||
|
* @version Ver1.0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum ResponseType {
|
||||||
|
/**
|
||||||
|
* json格式响应数据
|
||||||
|
*/
|
||||||
|
JSON,
|
||||||
|
/**
|
||||||
|
* html格式响应数据
|
||||||
|
*/
|
||||||
|
HTML,
|
||||||
|
/**
|
||||||
|
* xml格式响应数据
|
||||||
|
*/
|
||||||
|
XML,
|
||||||
|
/**
|
||||||
|
* 纯文本格式响应数据
|
||||||
|
*/
|
||||||
|
TEXT,
|
||||||
|
/**
|
||||||
|
* 无响应数据
|
||||||
|
*/
|
||||||
|
EMPTY;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,102 @@
|
||||||
|
package pres.auxiliary.tool.http;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p><b>文件名:</b>HeadType.java</p>
|
||||||
|
* <p><b>用途:</b>
|
||||||
|
* 枚举部分请求头配置
|
||||||
|
* </p>
|
||||||
|
* <p><b>编码时间:</b>2020年6月22日上午9:05:07</p>
|
||||||
|
* <p><b>修改时间:</b>2020年6月22日上午9:05:07</p>
|
||||||
|
* @author 彭宇琦
|
||||||
|
* @version Ver1.0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum HeadType {
|
||||||
|
/**
|
||||||
|
* json协议
|
||||||
|
*/
|
||||||
|
CONTENT_TYPE_JSON("Content-Type", "application/json"),
|
||||||
|
/**
|
||||||
|
* 表单
|
||||||
|
*/
|
||||||
|
CONTENT_TYPE_URLENCODED("Content-Type", "application/x-www-form-urlencoded"),
|
||||||
|
/**
|
||||||
|
* 表单文件
|
||||||
|
*/
|
||||||
|
CONTENT_TYPE_FROM_DATA("Content-Type", "multipart/form-data"),
|
||||||
|
/**
|
||||||
|
* 纯文本
|
||||||
|
*/
|
||||||
|
CONTENT_TYPE_PLAIN("Content-Type", "text/plain"),
|
||||||
|
/**
|
||||||
|
* html文本
|
||||||
|
*/
|
||||||
|
CONTENT_TYPE_HTML("Content-Type", "text/html"),
|
||||||
|
/**
|
||||||
|
* soap协议
|
||||||
|
*/
|
||||||
|
CONTENT_TYPE_SOAP("Content-Type", "application/soap+xml"),
|
||||||
|
/**
|
||||||
|
* 文件
|
||||||
|
*/
|
||||||
|
CONTENT_TYPE_FILE("Content-Type", "application/file"),
|
||||||
|
;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求头名称
|
||||||
|
*/
|
||||||
|
String key;
|
||||||
|
/**
|
||||||
|
* 请求头值
|
||||||
|
*/
|
||||||
|
String value;
|
||||||
|
/**
|
||||||
|
* 文字编码
|
||||||
|
*/
|
||||||
|
String encoding ="";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化枚举值
|
||||||
|
* @param key 请求头名称
|
||||||
|
* @param value 请求头值
|
||||||
|
*/
|
||||||
|
private HeadType(String key, String value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回请求头名称
|
||||||
|
* @return 请求头名称
|
||||||
|
*/
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回请求头的值
|
||||||
|
* @return 请求头的值
|
||||||
|
*/
|
||||||
|
public String getValue() {
|
||||||
|
return encoding.isEmpty() ? value : (value + ";" + encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于设置请求的编码格式
|
||||||
|
* @param encoding 编码格式
|
||||||
|
* @return 枚举本身
|
||||||
|
*/
|
||||||
|
public HeadType setEncoding(String encoding) {
|
||||||
|
this.encoding = "charset=" + encoding;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于清除当前设置的编码格式
|
||||||
|
* @return 枚举本身
|
||||||
|
*/
|
||||||
|
public HeadType clearEncoding() {
|
||||||
|
encoding = "";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,38 +1,88 @@
|
||||||
package pres.auxiliary.tool.http;
|
package pres.auxiliary.tool.http;
|
||||||
|
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterMethod;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
public class EasyHttpTest {
|
public class EasyHttpTest {
|
||||||
EasyHttp eh = new EasyHttp();
|
EasyHttp eh = new EasyHttp();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示url
|
||||||
|
*/
|
||||||
@AfterMethod
|
@AfterMethod
|
||||||
public void showUrl() {
|
public void showUrl() {
|
||||||
System.out.println(eh.getUrlString());
|
System.out.println(eh.getUrlString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#url(String)}方法,地址全面
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void urlTest_All() {
|
public void urlTest_All() {
|
||||||
eh.url("http://127.0.0.1:8080/a/b");
|
eh.url("http://127.0.0.1:8080/a/b?a=1&w=2 5");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#url(String)}方法,无协议
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void urlTest_NotAgreement() {
|
public void urlTest_NotAgreement() {
|
||||||
eh.url("127.0.0.1:8080/a/b");
|
eh.url("127.0.0.1:8080/a/b");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#url(String)}方法,无端口
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void urlTest_NotPort() {
|
public void urlTest_NotPort() {
|
||||||
eh.url("http://127.0.0.1/a/b");
|
eh.url("http://127.0.0.1/a/b");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#url(String)}方法,无请求路径
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void urlTest_NotAddress() {
|
public void urlTest_NotAddress() {
|
||||||
eh.url("http://127.0.0.1:8080");
|
eh.url("http://127.0.0.1:8080");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#url(String)}方法,域名
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void urlTest_Host() {
|
public void urlTest_Host() {
|
||||||
eh.url("http://www.hao123.com/a/b");
|
eh.url("http://www.hao123.com/a/b");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#agreement(String)}方法
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void agreementTest() {
|
||||||
|
eh.agreement("ftp://");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试不做任何参数处理时的返回
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void notSetTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#host(String)}方法
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void hostTest() {
|
||||||
|
eh.host("10.125.163.144");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试{@link EasyHttp#port(String)}方法
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void portTest() {
|
||||||
|
eh.port("55663");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue