增加HTTP接口测试的patch、delete(支持JSON格式)两种类型请求

This commit is contained in:
seagull 2020-01-15 16:08:09 +08:00
parent 01320bb910
commit ea3d0e067a
6 changed files with 231 additions and 8 deletions

View File

@ -22,7 +22,7 @@
<img alt="code style" src="https://img.shields.io/badge/BUILD-PASSING-green.svg">
</a>
<a href="http://git.oschina.net/seagull1985/LuckyFrameWeb/releases">
<img src="https://img.shields.io/badge/LuckyFrame-V3.1.1 releases-green.svg" >
<img src="https://img.shields.io/badge/LuckyFrame-V3.2 releases-green.svg" >
</a>
</p>
@ -75,7 +75,7 @@
> [【直达官网查看版本更新文章】](http://www.luckyframe.cn/dynamic.html)
# 大家安静下,我再说一句
此项目是LuckyFrame客户端项目此外还有一个LuckyFrameWeb的项目做为服务端如果您只需要先看看界面那么单独下载LuckyFrameWeb项目就够了如果您需要搭建整套环境那还要把此LuckyFrame客户端项目下载下来。
> 此项目是LuckyFrame客户端项目此外还有一个[LuckyFrameWeb的项目](https://gitee.com/seagull1985/LuckyFrameWeb)做为服务端如果您只需要先看看界面那么单独下载LuckyFrameWeb项目就够了如果您需要搭建整套环境那还要把此LuckyFrame客户端项目下载下来。
# 期待您更多的意见

View File

@ -276,12 +276,6 @@
<artifactId>oshi-core</artifactId>
<version>3.9.1</version>
</dependency>
<!-- 获取系统信息 -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@ -412,4 +412,11 @@ public class SubString {
return jsonString;
}
public static void main(String[] args) {
String json = " "
+ "{\"access_token\":\"d26b9148-20c9-4779-888c-0d9988cf3bf5\",\"token_type\":\"bearer\",\"expires_in\":599999,\"scope\":\"1/platformehicle/access_gateway/engine/forbid,1/platformehicle/access_gateway/engine/enable 1/platformehicle/access_gateway/system/diagnosis\"} ";
String key = "access_token";
String indexstr = "1";
System.out.println(getJsonValue(json, key, indexstr));
}
}

View File

@ -233,6 +233,10 @@ public class InvokeMethod {
result = HttpClientTools.httpClientPostJson(packagename, params, headmsg , ppt);
} else if (functionname.toLowerCase().equals("httpurldelete")) {
result = HttpClientTools.sendHttpURLDel(packagename, params, headmsg,ppt);
} else if (functionname.toLowerCase().equals("httpclientdeletejson")) {
result = HttpClientTools.httpClientDeleteJson(packagename, params, headmsg,ppt);
} else if (functionname.toLowerCase().equals("httpclientpatchjson")) {
result = HttpClientTools.httpClientPatchJson(packagename, params, headmsg, ppt);
} else if (functionname.toLowerCase().equals("httpclientputjson")) {
result = HttpClientTools.httpClientPutJson(packagename, params, headmsg , ppt);
} else if (functionname.toLowerCase().equals("httpclientput")) {

View File

@ -37,6 +37,7 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.config.Registry;
@ -1453,7 +1454,204 @@ public class HttpClientTools {
return resultBuffer.toString();
}
/**
* 使用HttpClient发送Delete请求 参数JSON格式
* @param urlParam
* @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
*/
public static String httpClientDeleteJson(String urlParam, Map<String, Object> params, Map<String, String> headmsg,ProjectProtocolTemplate ppt) throws KeyManagementException, NoSuchAlgorithmException {
String cerpath=ppt.getCerificatePath();
String charset=ppt.getEncoding().toLowerCase();
int timeout=ppt.getTimeout()*1000;
int responsehead=ppt.getIsResponseHead();
int responsecode=ppt.getIsResponseCode();
StringBuffer resultBuffer = null;
LogUtil.APP.info("设置HTTP请求地址:【{}】",urlParam);
CloseableHttpClient httpclient=iniHttpClient(urlParam,cerpath);
HttpDeleteWithBody httpDel = new HttpDeleteWithBody(urlParam);
httpDel.setHeader("Content-Type", "application/json");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
//设置请求和传输超时时间
.setSocketTimeout(timeout).build();
httpDel.setConfig(requestConfig);
//替换头域信息
for (Map.Entry<String, String> m :headmsg.entrySet()) {
String key=m.getKey();
String value=m.getValue();
LogUtil.APP.info("开始设置|替换HTTP头域信息...key:【{}】 value:【{}】",key,value);
if(null!=value&&value.indexOf("Base64(")==0){
String valuesub=value.substring(value.indexOf("Base64(")+7,value.lastIndexOf(")"));
value="Basic " + DatatypeConverter.printBase64Binary((valuesub).getBytes());
LogUtil.APP.info("将头域【{}】的值【{}】FORMAT成BASE64格式...",key,value);
httpDel.setHeader(key,value);
}else{
httpDel.setHeader(key,value);
}
}
// 构建请求参数
BufferedReader br = null;
try {
if(params.size()>0){
if(1==params.size()&&params.containsKey("_forTextJson")){
LogUtil.APP.info("参数类型TEXT,设置httpClientDeleteJson参数信息...【{}】",params.get("_forTextJson").toString());
StringEntity entity = new StringEntity(params.get("_forTextJson").toString(),charset);
httpDel.setEntity(entity);
}else{
String jsonString = JSON.toJSONString(params);
LogUtil.APP.info("参数类型FORM,设置httpClientDeleteJson参数信息...【{}】",jsonString);
StringEntity entity = new StringEntity(jsonString,charset);
httpDel.setEntity(entity);
}
}
CloseableHttpResponse response = httpclient.execute(httpDel);
// 读取服务器响应数据
resultBuffer = new StringBuffer();
if(1==responsehead){
Header[] headmsgstr=response.getAllHeaders();
resultBuffer.append("RESPONSE_HEAD:【{");
for(Header header:headmsgstr){
resultBuffer.append("\""+header.getName()+"\":\""+header.getValue()+"\",");
}
resultBuffer.delete(resultBuffer.length()-1, resultBuffer.length()).append("}】 ");
}
if(1==responsecode){
resultBuffer.append(Constants.RESPONSE_CODE+response.getStatusLine().getStatusCode()+Constants.RESPONSE_END);
}
if(null!=response.getEntity()){
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
String temp;
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}
}
} catch (Exception e) {
LogUtil.APP.error("使用HttpClient发送Delete请求(参数JSON格式)出现异常,请检查!", e);
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LogUtil.APP.error("使用HttpClient发送Delete请求(参数JSON格式)后关闭br流出现异常请检查", e);
br = null;
throw new RuntimeException(e);
}
}
}
return resultBuffer.toString();
}
/**
* 使用HttpClient发送Patch请求 参数JSON格式
* @param urlParam
* @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
*/
public static String httpClientPatchJson(String urlParam, Map<String, Object> params, Map<String, String> headmsg,ProjectProtocolTemplate ppt) throws KeyManagementException, NoSuchAlgorithmException {
String cerpath=ppt.getCerificatePath();
String charset=ppt.getEncoding().toLowerCase();
int timeout=ppt.getTimeout()*1000;
int responsehead=ppt.getIsResponseHead();
int responsecode=ppt.getIsResponseCode();
StringBuffer resultBuffer = null;
LogUtil.APP.info("设置HTTP请求地址:【{}】",urlParam);
CloseableHttpClient httpclient=iniHttpClient(urlParam,cerpath);
HttpPatch httpput = new HttpPatch(urlParam);
httpput.setHeader("Content-Type", "application/json");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
//设置请求和传输超时时间
.setSocketTimeout(timeout).build();
httpput.setConfig(requestConfig);
//替换头域信息
for (Map.Entry<String, String> m :headmsg.entrySet()) {
String key=m.getKey();
String value=m.getValue();
LogUtil.APP.info("开始设置|替换HTTP头域信息...key:【{}】 value:【{}】",key,value);
if(null!=value&&value.indexOf("Base64(")==0){
String valuesub=value.substring(value.indexOf("Base64(")+7,value.lastIndexOf(")"));
value="Basic " + DatatypeConverter.printBase64Binary((valuesub).getBytes());
LogUtil.APP.info("将头域【{}】的值【{}】FORMAT成BASE64格式...",key,value);
httpput.setHeader(key,value);
}else{
httpput.setHeader(key,value);
}
}
// 构建请求参数
BufferedReader br = null;
try {
if(params.size()>0){
if(1==params.size()&&params.containsKey("_forTextJson")){
LogUtil.APP.info("参数类型TEXT,设置httpClientPatchJson参数信息...【{}】",params.get("_forTextJson").toString());
StringEntity entity = new StringEntity(params.get("_forTextJson").toString(),charset);
httpput.setEntity(entity);
}else{
String jsonString = JSON.toJSONString(params);
LogUtil.APP.info("参数类型FORM,设置httpClientPatchJson参数信息...【{}】",jsonString);
StringEntity entity = new StringEntity(jsonString,charset);
httpput.setEntity(entity);
}
}
CloseableHttpResponse response = httpclient.execute(httpput);
// 读取服务器响应数据
resultBuffer = new StringBuffer();
if(1==responsehead){
Header[] headmsgstr=response.getAllHeaders();
resultBuffer.append("RESPONSE_HEAD:【{");
for(Header header:headmsgstr){
resultBuffer.append("\""+header.getName()+"\":\""+header.getValue()+"\",");
}
resultBuffer.delete(resultBuffer.length()-1, resultBuffer.length()).append("}】 ");
}
if(1==responsecode){
resultBuffer.append(Constants.RESPONSE_CODE+response.getStatusLine().getStatusCode()+Constants.RESPONSE_END);
}
if(null!=response.getEntity()){
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
String temp;
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}
}
} catch (Exception e) {
LogUtil.APP.error("使用HttpClient发送Patch请求(参数JSON格式)出现异常,请检查!", e);
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LogUtil.APP.error("使用HttpClient发送Patch请求(参数JSON格式)后关闭br流出现异常请检查", e);
br = null;
throw new RuntimeException(e);
}
}
}
return resultBuffer.toString();
}
/**
* 使用HttpClient发送put请求 参数JSON格式
* @param urlParam

View File

@ -0,0 +1,20 @@
package luckyclient.utils.httputils;
import java.net.URI;
import javax.annotation.concurrent.NotThreadSafe;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
@NotThreadSafe
public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
public String getMethod() { return METHOD_NAME; }
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() { super(); }
}