From ea3d0e067ace7ce3e597af1809523945f55d7078 Mon Sep 17 00:00:00 2001
From: seagull <24163551@qq.com>
Date: Wed, 15 Jan 2020 16:08:09 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0HTTP=E6=8E=A5=E5=8F=A3?=
=?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=9A=84patch=E3=80=81delete=EF=BC=88?=
=?UTF-8?q?=E6=94=AF=E6=8C=81JSON=E6=A0=BC=E5=BC=8F=EF=BC=89=E4=B8=A4?=
=?UTF-8?q?=E7=A7=8D=E7=B1=BB=E5=9E=8B=E8=AF=B7=E6=B1=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 +-
pom.xml | 6 -
.../java/luckyclient/driven/SubString.java | 7 +
.../java/luckyclient/utils/InvokeMethod.java | 4 +
.../utils/httputils/HttpClientTools.java | 198 ++++++++++++++++++
.../utils/httputils/HttpDeleteWithBody.java | 20 ++
6 files changed, 231 insertions(+), 8 deletions(-)
create mode 100644 src/main/java/luckyclient/utils/httputils/HttpDeleteWithBody.java
diff --git a/README.md b/README.md
index 721fd4e..fe0382f 100644
--- a/README.md
+++ b/README.md
@@ -22,7 +22,7 @@
-
+
@@ -75,7 +75,7 @@
> [【直达官网查看版本更新文章】](http://www.luckyframe.cn/dynamic.html)
# 大家安静下,我再说一句
-此项目是LuckyFrame客户端项目,此外还有一个LuckyFrameWeb的项目做为服务端,如果您只需要先看看界面,那么单独下载LuckyFrameWeb项目就够了,如果您需要搭建整套环境,那还要把此LuckyFrame客户端项目下载下来。
+> 此项目是LuckyFrame客户端项目,此外还有一个[LuckyFrameWeb的项目](https://gitee.com/seagull1985/LuckyFrameWeb)做为服务端,如果您只需要先看看界面,那么单独下载LuckyFrameWeb项目就够了,如果您需要搭建整套环境,那还要把此LuckyFrame客户端项目下载下来。
# 期待您更多的意见
diff --git a/pom.xml b/pom.xml
index bf19c72..634d730 100644
--- a/pom.xml
+++ b/pom.xml
@@ -276,12 +276,6 @@
oshi-core
3.9.1
-
-
- com.github.oshi
- oshi-core
- 3.9.1
-
org.springframework.boot
spring-boot-starter-web
diff --git a/src/main/java/luckyclient/driven/SubString.java b/src/main/java/luckyclient/driven/SubString.java
index 79d248c..4e3036f 100644
--- a/src/main/java/luckyclient/driven/SubString.java
+++ b/src/main/java/luckyclient/driven/SubString.java
@@ -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));
+ }
}
diff --git a/src/main/java/luckyclient/utils/InvokeMethod.java b/src/main/java/luckyclient/utils/InvokeMethod.java
index 840b9d6..50a36d7 100644
--- a/src/main/java/luckyclient/utils/InvokeMethod.java
+++ b/src/main/java/luckyclient/utils/InvokeMethod.java
@@ -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")) {
diff --git a/src/main/java/luckyclient/utils/httputils/HttpClientTools.java b/src/main/java/luckyclient/utils/httputils/HttpClientTools.java
index 44b226d..fcc935c 100644
--- a/src/main/java/luckyclient/utils/httputils/HttpClientTools.java
+++ b/src/main/java/luckyclient/utils/httputils/HttpClientTools.java
@@ -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 params, Map 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 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 params, Map 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 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格式
* @param urlParam
diff --git a/src/main/java/luckyclient/utils/httputils/HttpDeleteWithBody.java b/src/main/java/luckyclient/utils/httputils/HttpDeleteWithBody.java
new file mode 100644
index 0000000..b9fdf2f
--- /dev/null
+++ b/src/main/java/luckyclient/utils/httputils/HttpDeleteWithBody.java
@@ -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(); }
+}