增加HTTP接口测试的patch、delete(支持JSON格式)两种类型请求
This commit is contained in:
parent
01320bb910
commit
ea3d0e067a
|
@ -22,7 +22,7 @@
|
||||||
<img alt="code style" src="https://img.shields.io/badge/BUILD-PASSING-green.svg">
|
<img alt="code style" src="https://img.shields.io/badge/BUILD-PASSING-green.svg">
|
||||||
</a>
|
</a>
|
||||||
<a href="http://git.oschina.net/seagull1985/LuckyFrameWeb/releases">
|
<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>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@
|
||||||
> [【直达官网查看版本更新文章】](http://www.luckyframe.cn/dynamic.html)
|
> [【直达官网查看版本更新文章】](http://www.luckyframe.cn/dynamic.html)
|
||||||
|
|
||||||
# 大家安静下,我再说一句
|
# 大家安静下,我再说一句
|
||||||
此项目是LuckyFrame客户端项目,此外还有一个LuckyFrameWeb的项目做为服务端,如果您只需要先看看界面,那么单独下载LuckyFrameWeb项目就够了,如果您需要搭建整套环境,那还要把此LuckyFrame客户端项目下载下来。
|
> 此项目是LuckyFrame客户端项目,此外还有一个[LuckyFrameWeb的项目](https://gitee.com/seagull1985/LuckyFrameWeb)做为服务端,如果您只需要先看看界面,那么单独下载LuckyFrameWeb项目就够了,如果您需要搭建整套环境,那还要把此LuckyFrame客户端项目下载下来。
|
||||||
|
|
||||||
# 期待您更多的意见
|
# 期待您更多的意见
|
||||||
|
|
||||||
|
|
6
pom.xml
6
pom.xml
|
@ -276,12 +276,6 @@
|
||||||
<artifactId>oshi-core</artifactId>
|
<artifactId>oshi-core</artifactId>
|
||||||
<version>3.9.1</version>
|
<version>3.9.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- 获取系统信息 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.oshi</groupId>
|
|
||||||
<artifactId>oshi-core</artifactId>
|
|
||||||
<version>3.9.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|
|
@ -412,4 +412,11 @@ public class SubString {
|
||||||
return jsonString;
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -233,6 +233,10 @@ public class InvokeMethod {
|
||||||
result = HttpClientTools.httpClientPostJson(packagename, params, headmsg , ppt);
|
result = HttpClientTools.httpClientPostJson(packagename, params, headmsg , ppt);
|
||||||
} else if (functionname.toLowerCase().equals("httpurldelete")) {
|
} else if (functionname.toLowerCase().equals("httpurldelete")) {
|
||||||
result = HttpClientTools.sendHttpURLDel(packagename, params, headmsg,ppt);
|
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")) {
|
} else if (functionname.toLowerCase().equals("httpclientputjson")) {
|
||||||
result = HttpClientTools.httpClientPutJson(packagename, params, headmsg , ppt);
|
result = HttpClientTools.httpClientPutJson(packagename, params, headmsg , ppt);
|
||||||
} else if (functionname.toLowerCase().equals("httpclientput")) {
|
} else if (functionname.toLowerCase().equals("httpclientput")) {
|
||||||
|
|
|
@ -37,6 +37,7 @@ import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
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.HttpPost;
|
||||||
import org.apache.http.client.methods.HttpPut;
|
import org.apache.http.client.methods.HttpPut;
|
||||||
import org.apache.http.config.Registry;
|
import org.apache.http.config.Registry;
|
||||||
|
@ -1453,6 +1454,203 @@ public class HttpClientTools {
|
||||||
return resultBuffer.toString();
|
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()&¶ms.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()&¶ms.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格式
|
* 使用HttpClient发送put请求 参数JSON格式
|
||||||
|
|
|
@ -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(); }
|
||||||
|
}
|
Loading…
Reference in New Issue