mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 02:25:56 +08:00
添加 海康设备http请求方法
This commit is contained in:
parent
bf6cb1ed50
commit
d1c15325e6
@ -33,6 +33,7 @@
|
||||
<module>ruoyi-common-encrypt</module>
|
||||
<module>ruoyi-common-tenant</module>
|
||||
<module>ruoyi-common-websocket</module>
|
||||
<module>ruoyi-common-hik</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
|
||||
65
ruoyi-common/ruoyi-common-hik/pom.xml
Normal file
65
ruoyi-common/ruoyi-common-hik/pom.xml
Normal file
@ -0,0 +1,65 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<parent>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>ruoyi-common-hik</artifactId>
|
||||
<description>
|
||||
ruoyi-common-json 序列化模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.14</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient-cache</artifactId>
|
||||
<version>4.5.14</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>4.4.16</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore-nio</artifactId>
|
||||
<version>4.4.16</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpasyncclient</artifactId>
|
||||
<version>4.1.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpasyncclient-cache</artifactId>
|
||||
<version>4.1.5</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JSON XML工具类 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,126 @@
|
||||
package com.ruoyi.hik.commom;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* HttpClientProperties
|
||||
*
|
||||
* @author xuzhou
|
||||
* @version v1.0.0
|
||||
* @create 2021/7/22 16:35
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "http.client")
|
||||
public class HttpClientConfig {
|
||||
/**
|
||||
* 返回从连接管理器请求连接时使用的超时时间(以毫秒为单位)。
|
||||
* 默认值: -1,为无限超时。
|
||||
*/
|
||||
private int connectionRequestTimeout = 5000;
|
||||
|
||||
/**
|
||||
* 连接超时:连接一个url的连接等待时间
|
||||
* 确定建立连接之前的超时时间(以毫秒为单位)。
|
||||
* 默认值: -1,为无限超时。
|
||||
*/
|
||||
private int connectTimeout = 5000;
|
||||
|
||||
/**
|
||||
* 读取数据超时:连上url,获取response的返回等待时间
|
||||
* 以毫秒为单位定义套接字超时,它是等待数据的超时,或者换句话说,两个连续数据包之间的最长不活动时间。
|
||||
* 默认值: -1,为无限超时。
|
||||
*/
|
||||
private int socketTimeout = 5000;
|
||||
|
||||
/**
|
||||
* 客户端总并行链接最大数
|
||||
*/
|
||||
private int maxTotal = 300;
|
||||
|
||||
/**
|
||||
* 客户端每个路由最高链接最大数
|
||||
*/
|
||||
private int maxPreRoute = 5;
|
||||
|
||||
/**
|
||||
* 连接存活时长:秒
|
||||
*/
|
||||
private long connectionTimeToLive = 60;
|
||||
|
||||
/**
|
||||
* 重试尝试最大次数
|
||||
* 默认为3
|
||||
*/
|
||||
private int retryCount = 3;
|
||||
|
||||
/**
|
||||
* 非幂等请求是否可以重试
|
||||
* 默认不开启
|
||||
*/
|
||||
private boolean requestSentRetryEnabled = false;
|
||||
|
||||
public int getConnectionRequestTimeout() {
|
||||
return connectionRequestTimeout;
|
||||
}
|
||||
|
||||
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
|
||||
this.connectionRequestTimeout = connectionRequestTimeout;
|
||||
}
|
||||
|
||||
public int getConnectTimeout() {
|
||||
return connectTimeout;
|
||||
}
|
||||
|
||||
public void setConnectTimeout(int connectTimeout) {
|
||||
this.connectTimeout = connectTimeout;
|
||||
}
|
||||
|
||||
public int getSocketTimeout() {
|
||||
return socketTimeout;
|
||||
}
|
||||
|
||||
public void setSocketTimeout(int socketTimeout) {
|
||||
this.socketTimeout = socketTimeout;
|
||||
}
|
||||
|
||||
public int getMaxTotal() {
|
||||
return maxTotal;
|
||||
}
|
||||
|
||||
public void setMaxTotal(int maxTotal) {
|
||||
this.maxTotal = maxTotal;
|
||||
}
|
||||
|
||||
public int getMaxPreRoute() {
|
||||
return maxPreRoute;
|
||||
}
|
||||
|
||||
public void setMaxPreRoute(int maxPreRoute) {
|
||||
this.maxPreRoute = maxPreRoute;
|
||||
}
|
||||
|
||||
public long getConnectionTimeToLive() {
|
||||
return connectionTimeToLive;
|
||||
}
|
||||
|
||||
public void setConnectionTimeToLive(long connectionTimeToLive) {
|
||||
this.connectionTimeToLive = connectionTimeToLive;
|
||||
}
|
||||
|
||||
public int getRetryCount() {
|
||||
return retryCount;
|
||||
}
|
||||
|
||||
public void setRetryCount(int retryCount) {
|
||||
this.retryCount = retryCount;
|
||||
}
|
||||
|
||||
public boolean isRequestSentRetryEnabled() {
|
||||
return requestSentRetryEnabled;
|
||||
}
|
||||
|
||||
public void setRequestSentRetryEnabled(boolean requestSentRetryEnabled) {
|
||||
this.requestSentRetryEnabled = requestSentRetryEnabled;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,331 @@
|
||||
package com.ruoyi.hik.communicationCom;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import com.ruoyi.hik.commom.HttpClientConfig;
|
||||
import com.ruoyi.hik.domain.HikEquipmentInfo;
|
||||
import com.ruoyi.hik.domain.HikSecurity;
|
||||
import com.ruoyi.hik.domain.HikSecurityUserCheck;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.*;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class HTTPClientUtil {
|
||||
|
||||
public static CopyOnWriteArraySet<HTTPClientUtil> copyOnWriteArraySet = new CopyOnWriteArraySet<HTTPClientUtil>();
|
||||
|
||||
//创建XmlMapper对象,用于实体与json和xml之间的相互转换
|
||||
private static final XmlMapper xmlMapper = new XmlMapper();
|
||||
|
||||
/**
|
||||
* 配置类
|
||||
*/
|
||||
private static final HttpClientConfig HTTP_CLIENT_CONFIG = new HttpClientConfig();;
|
||||
|
||||
// 使用工厂类 HttpClients 进行创建
|
||||
// 1、默认配置创建
|
||||
private CloseableHttpClient httpClient;
|
||||
private HikEquipmentInfo equipmentInfo;
|
||||
|
||||
/**
|
||||
* 请求配置
|
||||
*/
|
||||
private static RequestConfig requestConfig;
|
||||
|
||||
// 设置要访问的HttpHost,即是目标站点的HttpHost
|
||||
private HttpHost target = null;
|
||||
|
||||
public static HTTPClientUtil getHikEquipmentHTTPClient(HikEquipmentInfo equipment) {
|
||||
for (HTTPClientUtil util : HTTPClientUtil.copyOnWriteArraySet) {
|
||||
if (util.getEquipmentInfo().getEquipmentIp().equals(equipment.getEquipmentIp())) {
|
||||
return util;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public HikSecurityUserCheck doLogin(HikEquipmentInfo equipment) throws IOException {
|
||||
requestConfig = RequestConfig.custom()
|
||||
.setAuthenticationEnabled(true)
|
||||
.setConnectTimeout(HTTP_CLIENT_CONFIG.getConnectTimeout())
|
||||
.setConnectionRequestTimeout(HTTP_CLIENT_CONFIG.getConnectionRequestTimeout())
|
||||
.setSocketTimeout(HTTP_CLIENT_CONFIG.getSocketTimeout())
|
||||
.build();
|
||||
this.setTarget( new HttpHost(equipment.getEquipmentIp(), 80));
|
||||
|
||||
// 2、使用 builder来创建,可以添加自定义配置
|
||||
// 自定义 connectionManager 连接管理器
|
||||
// 配置连接池关联
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
||||
connectionManager.setMaxTotal(HTTP_CLIENT_CONFIG.getMaxTotal());
|
||||
connectionManager.setDefaultMaxPerRoute(HTTP_CLIENT_CONFIG.getMaxPreRoute());
|
||||
|
||||
// 配置登录权限相关
|
||||
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(equipment.getUsername(), equipment.getPassword());
|
||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(AuthScope.ANY,credentials);
|
||||
|
||||
|
||||
// 初始化客户端
|
||||
httpClient = HttpClients.custom()
|
||||
.setConnectionManager(connectionManager)
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setDefaultCredentialsProvider(credentialsProvider)
|
||||
// 重试机制
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_CONFIG.getRetryCount(), HTTP_CLIENT_CONFIG.isRequestSentRetryEnabled()))
|
||||
// 开启后台线程清除过期的连接
|
||||
.evictExpiredConnections()
|
||||
// 开启后台线程清除闲置的连接
|
||||
.evictIdleConnections(HTTP_CLIENT_CONFIG.getConnectionTimeToLive(), TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
|
||||
/**
|
||||
* <userCheck xmlns="http://www.isapi.org/ver20/XMLSchema" version="2.0">
|
||||
* <!--ro, req, object, 用户名密码匹配, attr:version{req, string, 协议版本}-->
|
||||
* <statusValue>
|
||||
* <!--ro, req, enum, 校验结果状态, subType:int, [200#200,401#401]-->200
|
||||
* </statusValue>
|
||||
* <statusString>
|
||||
* <!--ro, opt, enum, 校验结果状态字符信息, subType:string, [OK#OK,Unauthorized#Unauthorized]-->OK
|
||||
* </statusString>
|
||||
* <isDefaultPassword>
|
||||
* <!--ro, opt, bool, 是否是默认密码-->true
|
||||
* </isDefaultPassword>
|
||||
* <isRiskPassword>
|
||||
* <!--ro, opt, bool, 是否是风险密码-->true
|
||||
* </isRiskPassword>
|
||||
* <isActivated>
|
||||
* <!--ro, opt, bool, 是否已激活-->true
|
||||
* </isActivated>
|
||||
* <residualValidity>
|
||||
* <!--ro, opt, int, 密码剩余有效天数, desc:返回负值,表示密码已经超期使用,例如"-3表示密码已经超期使用3天-->1
|
||||
* </residualValidity>
|
||||
* <lockStatus>
|
||||
* <!--ro, opt, enum, 锁定状态, subType:string, [unlock#未锁定,locked#已锁定]-->unlock
|
||||
* </lockStatus>
|
||||
* <unlockTime>
|
||||
* <!--ro, opt, int, 解锁剩余时间, unit:s, unitType:时间-->1
|
||||
* </unlockTime>
|
||||
* <retryLoginTime>
|
||||
* <!--ro, opt, int, 重试次数-->1
|
||||
* </retryLoginTime>
|
||||
* </userCheck>
|
||||
*/
|
||||
String url = "/ISAPI/Security/userCheck";
|
||||
|
||||
HikSecurity security = xmlMapper.readValue(this.doGet(url), HikSecurity.class);
|
||||
if(security.getUserCheck().getStatusValue() ==200 ){
|
||||
copyOnWriteArraySet.add(this);
|
||||
this.setEquipmentInfo(equipment);
|
||||
}
|
||||
|
||||
return security.getUserCheck();
|
||||
|
||||
}
|
||||
|
||||
public String doGet(String url) throws IOException {
|
||||
String result = null;
|
||||
HttpGet method = new HttpGet(url);
|
||||
method.setConfig(requestConfig);
|
||||
//response 对象
|
||||
HttpResponse response = this.httpClient.execute(this.getTarget(), method);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
System.out.println(response);
|
||||
if (statusCode != 200) {
|
||||
method.abort();
|
||||
// throw new ServiceException("请求设备失败 错误码:" + statusCode);
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
// Release the connection
|
||||
// method.reset();
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public HttpResponse doPut(String url, String inbound) throws Exception {
|
||||
|
||||
HttpPut method = new HttpPut(url);
|
||||
method.setConfig(requestConfig);
|
||||
|
||||
// method.setRequestBody(inbound);
|
||||
StringEntity stringEntity = new StringEntity(inbound);
|
||||
method.setEntity(stringEntity);
|
||||
|
||||
//response 对象
|
||||
HttpResponse response = null;
|
||||
response = this.httpClient.execute(this.getTarget(), method);
|
||||
// Release the connection
|
||||
// method.reset();
|
||||
return response;
|
||||
}
|
||||
|
||||
public String doPost(String url, String inbound) throws Exception {
|
||||
String result = null;
|
||||
HttpPost method = new HttpPost(url);
|
||||
method.setConfig(requestConfig);
|
||||
StringEntity stringEntity = new StringEntity(inbound);
|
||||
method.setEntity(stringEntity);
|
||||
|
||||
//response 对象
|
||||
HttpResponse response = this.httpClient.execute(this.getTarget(), method);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
method.abort();
|
||||
// throw new ServiceException("请求设备失败 错误码:" + statusCode);
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, "utf-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
|
||||
// Release the connection
|
||||
// method.reset();
|
||||
return result;
|
||||
}
|
||||
|
||||
public HttpResponse doDelete(String url) throws Exception {
|
||||
|
||||
HttpDelete method = new HttpDelete(url);
|
||||
method.setConfig(requestConfig);
|
||||
|
||||
//response 对象
|
||||
HttpResponse response = null;
|
||||
response = this.httpClient.execute(this.getTarget(), method);
|
||||
// Release the connection
|
||||
// method.reset();
|
||||
return response;
|
||||
}
|
||||
|
||||
//With the binary form POst method
|
||||
public HttpResponse doPostwithBinaryData(String url, String json, String jsonName, String image, String imageName, String boundary) throws Exception {
|
||||
|
||||
HttpPost method = new HttpPost(url);
|
||||
method.setConfig(requestConfig);
|
||||
|
||||
method.setHeader("Accept", "text/html, application/xhtml+xml");
|
||||
method.setHeader("Accept-Language", "zh-CN");
|
||||
method.setHeader("Content-Type","multipart/form-data; boundary=" + boundary);
|
||||
method.setHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
|
||||
method.setHeader("Accept-Encoding","gzip, deflate");
|
||||
method.setHeader("Connection","Keep-Alive");
|
||||
method.setHeader("Cache-Control","no-cache");
|
||||
|
||||
String bodyParam =
|
||||
"--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\""+jsonName+"\";\r\n"
|
||||
+ "Content-Type: text/json\r\n"
|
||||
+ "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n"
|
||||
+ json + "\r\n"
|
||||
+ "--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\""+imageName+"\";\r\n"
|
||||
+ "Content-Type: image/jpeg\r\n"
|
||||
+ "Content-Length: " + Integer.toString(image.length()) + "\r\n\r\n"
|
||||
+ image
|
||||
+ "\r\n--" + boundary + "--\r\n";
|
||||
|
||||
//method.setRequestBody(bodyParam);
|
||||
StringEntity stringEntity = new StringEntity(bodyParam);
|
||||
method.setEntity(stringEntity);
|
||||
|
||||
|
||||
//response 对象
|
||||
HttpResponse response = null;
|
||||
response = this.httpClient.execute(this.getTarget(), method);
|
||||
// Release the connection
|
||||
// method.reset();
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponse doPostStorageCloud(String url, String json,String faceimage,String boundary) throws Exception {
|
||||
|
||||
HttpPost method = new HttpPost(url);
|
||||
method.setConfig(requestConfig);
|
||||
|
||||
method.setHeader("Accept", "text/html, application/xhtml+xml");
|
||||
method.setHeader("Accept-Language", "zh-CN");
|
||||
method.setHeader("Content-Type","multipart/form-data; boundary=" + boundary);
|
||||
method.setHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
|
||||
method.setHeader("Accept-Encoding","gzip, deflate");
|
||||
method.setHeader("Connection","Keep-Alive");
|
||||
method.setHeader("Cache-Control","no-cache");
|
||||
|
||||
String bodyParam =
|
||||
"--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\"uploadStorageCloud\";\r\n"
|
||||
+ "Content-Type: text/json\r\n"
|
||||
+ "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n"
|
||||
+ json + "\r\n"
|
||||
+ "--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\"imageData\";\r\n"
|
||||
+ "Content-Type: image/jpeg\r\n"
|
||||
+ "Content-Length: " + Integer.toString(faceimage.length()) + "\r\n\r\n"
|
||||
+ faceimage
|
||||
+ "\r\n--" + boundary + "--\r\n";
|
||||
|
||||
// method.setRequestBody(bodyParam);
|
||||
StringEntity stringEntity = new StringEntity(bodyParam);
|
||||
method.setEntity(stringEntity);
|
||||
|
||||
//response 对象
|
||||
HttpResponse response = null;
|
||||
response = this.httpClient.execute(this.getTarget(), method);
|
||||
// Release the connection
|
||||
// method.reset();
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
// public CloseableHttpClient getHttpClient() {
|
||||
// return httpClient;
|
||||
// }
|
||||
//
|
||||
// public void setHttpClient(CloseableHttpClient httpClient) {
|
||||
// this.httpClient = httpClient;
|
||||
// }
|
||||
|
||||
|
||||
public HikEquipmentInfo getEquipmentInfo() {
|
||||
return equipmentInfo;
|
||||
}
|
||||
|
||||
public void setEquipmentInfo(HikEquipmentInfo equipmentInfo) {
|
||||
this.equipmentInfo = equipmentInfo;
|
||||
}
|
||||
|
||||
public HttpHost getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public void setTarget(HttpHost target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,358 @@
|
||||
package com.ruoyi.hik.communicationCom;
|
||||
|
||||
import com.ruoyi.hik.commom.HttpClientConfig;
|
||||
import com.ruoyi.hik.domain.HttpResult;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* http连接工具
|
||||
* 不会对请求的结果做处理,用户可以访问 {@link HttpResult}
|
||||
* 通过{@linkplain HttpResult#getStatus()}判断响应码code
|
||||
* 通过{@linkplain HttpResult#getStringEntity()}获取响应实体字符串
|
||||
*
|
||||
* @author xuzhou
|
||||
* @version v1.0.0
|
||||
*/
|
||||
public class HttpClientUtils {
|
||||
|
||||
/**
|
||||
* Http客户端
|
||||
*/
|
||||
public static final CloseableHttpClient httpClient;
|
||||
|
||||
/**
|
||||
* 配置类
|
||||
*/
|
||||
private static final HttpClientConfig HTTP_CLIENT_CONFIG;
|
||||
|
||||
/**
|
||||
* 编码方式
|
||||
*/
|
||||
private static final String ENCODING = "utf-8";
|
||||
|
||||
/**
|
||||
* 日志对象
|
||||
*/
|
||||
// private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
|
||||
|
||||
/**
|
||||
* 请求配置
|
||||
*/
|
||||
private static final RequestConfig request_config;
|
||||
|
||||
static {
|
||||
// 配置类
|
||||
HTTP_CLIENT_CONFIG = new HttpClientConfig();
|
||||
|
||||
// 配置请求参数,请求时常,连接市场,读取数据时长
|
||||
request_config = RequestConfig.custom()
|
||||
.setAuthenticationEnabled(true)
|
||||
.setConnectTimeout(HTTP_CLIENT_CONFIG.getConnectTimeout())
|
||||
.setConnectionRequestTimeout(HTTP_CLIENT_CONFIG.getConnectionRequestTimeout())
|
||||
.setSocketTimeout(HTTP_CLIENT_CONFIG.getSocketTimeout())
|
||||
.build();
|
||||
|
||||
// 配置连接池关联
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
||||
connectionManager.setMaxTotal(HTTP_CLIENT_CONFIG.getMaxTotal());
|
||||
connectionManager.setDefaultMaxPerRoute(HTTP_CLIENT_CONFIG.getMaxPreRoute());
|
||||
|
||||
// 初始化客户端
|
||||
httpClient = HttpClients.custom()
|
||||
.setConnectionManager(connectionManager)
|
||||
.setDefaultRequestConfig(request_config)
|
||||
// 重试机制
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_CONFIG.getRetryCount(), HTTP_CLIENT_CONFIG.isRequestSentRetryEnabled()))
|
||||
// 开启后台线程清除过期的连接
|
||||
.evictExpiredConnections()
|
||||
// 开启后台线程清除闲置的连接
|
||||
.evictIdleConnections(HTTP_CLIENT_CONFIG.getConnectionTimeToLive(), TimeUnit.SECONDS)
|
||||
.build();
|
||||
}
|
||||
|
||||
private HttpClientUtils() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
* 1.支持不带参数的请求
|
||||
* 2.支持参数拼接在URl中的请求
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @return 返回值
|
||||
*/
|
||||
public static HttpResult doGet(String url) {
|
||||
return doGet(url, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 带有参数的GET请求
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param params 请求参数
|
||||
* @return 返回值
|
||||
*/
|
||||
public static HttpResult doGet(String url, Map<String, Object> params) {
|
||||
return doGet(url, params, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get 请求:指定请求头,请求参数
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param headers 请求头参数
|
||||
* @param params 请求参数
|
||||
* @return HttpResult
|
||||
*/
|
||||
public static HttpResult doGet(String url, Map<String, Object> params, Map<String, String> headers) {
|
||||
|
||||
// log.info("Http GET 请求URL:{}", url);
|
||||
// log.info("Http GET 请求参数:{}", JSONObject.toJSONString(params));
|
||||
|
||||
try {
|
||||
// 创建访问对象地址
|
||||
URIBuilder uriBuilder = new URIBuilder(url);
|
||||
if (params != null && !params.isEmpty()) {
|
||||
// 构建在URL中的请求参数
|
||||
Set<? extends Map.Entry<?, ?>> entrySet = params.entrySet();
|
||||
for (Map.Entry<?, ?> entry : entrySet) {
|
||||
uriBuilder.addParameter((String) entry.getKey(), String.valueOf(entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(uriBuilder.build().toString());
|
||||
|
||||
// 封装请求头
|
||||
packageHeader(headers, httpGet);
|
||||
|
||||
return execute(httpGet);
|
||||
} catch (URISyntaxException e) {
|
||||
// log.error("Get请求构建URL失败", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行POST请求
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @return 返回值
|
||||
*/
|
||||
public static HttpResult doPost(String url) {
|
||||
return doPost(url, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行POST请求:有参数
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param params 请求参数
|
||||
* @return 返回值
|
||||
*/
|
||||
public static HttpResult doPost(String url, Map<String, Object> params) {
|
||||
return doPost(url, params, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行POST请求
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param headers 请求头
|
||||
* @param params 请求参数
|
||||
* @return 返回值
|
||||
*/
|
||||
public static HttpResult doPost(String url, Map<String, Object> params, Map<String, String> headers) {
|
||||
|
||||
// log.info("Http POST 请求URL:{}", url);
|
||||
// log.info("Http POST 请求参数:{}", JSONObject.toJSONString(params));
|
||||
|
||||
// 创建http POST请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
try {
|
||||
// 封装请求头
|
||||
packageHeader(headers, httpPost);
|
||||
|
||||
// 封装请求参数
|
||||
packageParam(params, httpPost);
|
||||
return execute(httpPost);
|
||||
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// log.error("POST请求参数编码异常", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* http post json数据
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param json 请求参数
|
||||
* @return 返回值
|
||||
*/
|
||||
public static HttpResult doPostJson(String url, String json) {
|
||||
return doPostJson(url, json, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* http post json数据
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param json 请求参数
|
||||
* @param headers 请求头
|
||||
* @return 返回值
|
||||
*/
|
||||
public static HttpResult doPostJson(String url, String json, Map<String, String> headers) {
|
||||
// log.info("Http post json请求URL:{}", url);
|
||||
// log.info("Http post json请求参数:{}", json);
|
||||
// 创建http POST请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()));
|
||||
httpPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "text/plain;charset=utf-8"));
|
||||
|
||||
// 封装请求头
|
||||
packageHeader(headers, httpPost);
|
||||
if (json != null) {
|
||||
// 构造一个JSON请求的实体
|
||||
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
|
||||
// 将请求实体设置到httpPost对象中
|
||||
httpPost.setEntity(stringEntity);
|
||||
}
|
||||
return execute(httpPost);
|
||||
}
|
||||
|
||||
/**
|
||||
* http post stream请求
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param in 输入流
|
||||
* @return 返回数据
|
||||
*/
|
||||
public static HttpResult doPostInputStream(String url, InputStream in) {
|
||||
// 创建http POST请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()));
|
||||
httpPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "text/plain;charset=utf-8"));
|
||||
|
||||
if (in != null) {
|
||||
httpPost.setEntity(new InputStreamEntity(in));
|
||||
}
|
||||
|
||||
return execute(httpPost);
|
||||
}
|
||||
|
||||
/**
|
||||
* http post text请求
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @param text 文本内容
|
||||
* @return 返回数据
|
||||
*/
|
||||
public static HttpResult doPostWrite(String url, String text) {
|
||||
// 创建http POST请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()));
|
||||
httpPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "text/plain;charset=utf-8"));
|
||||
|
||||
// if (StringUtils.isNotBlank(text)) {
|
||||
// StringEntity stringEntity = new StringEntity(text, ContentType.TEXT_PLAIN);
|
||||
// httpPost.setEntity(stringEntity);
|
||||
// }
|
||||
return execute(httpPost);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行HTTP请求
|
||||
*
|
||||
* @param request {@link HttpRequestBase} 请求
|
||||
* @return {@link HttpResult} 请求结果
|
||||
*/
|
||||
public static HttpResult execute(HttpRequestBase request) {
|
||||
// 执行http请求
|
||||
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
||||
// 构建返回实体
|
||||
return new HttpResult(response.getStatusLine().getStatusCode(),
|
||||
EntityUtils.toString(response.getEntity()));
|
||||
} catch (Exception e) {
|
||||
// log.error("http 请求异常", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将请求参数处理为 NameValuePair
|
||||
*
|
||||
* @param params 请求参数Map
|
||||
* @return List<NameValuePair>
|
||||
*/
|
||||
public static List<NameValuePair> convertParams2NVPS(Map<String, Object> params) {
|
||||
if (!params.isEmpty()) {
|
||||
List<NameValuePair> parameters = new ArrayList<>();
|
||||
params.forEach((key, value) -> parameters.add(new BasicNameValuePair(key, String.valueOf(value))));
|
||||
return parameters;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装请求头
|
||||
*
|
||||
* @param headers 请求头参数列表
|
||||
* @param httpMethod 请求方式
|
||||
*/
|
||||
public static void packageHeader(Map<String, String> headers, HttpRequestBase httpMethod) {
|
||||
// if (MapUtils.isNotEmpty(headers)) {
|
||||
// Set<Map.Entry<String, String>> entrySet = headers.entrySet();
|
||||
// for (Map.Entry<String, String> entry : entrySet) {
|
||||
// // 设置请求头到 HttpRequestBase 对象中
|
||||
// httpMethod.setHeader(entry.getKey(), entry.getValue());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装请求参数
|
||||
*
|
||||
* @param params 请求参数
|
||||
* @param httpMethod 请求方式
|
||||
* @throws UnsupportedEncodingException 不支持字符编码异常
|
||||
*/
|
||||
private static void packageParam(Map<String, Object> params, HttpEntityEnclosingRequest httpMethod)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
// if (MapUtils.isNotEmpty(params)) {
|
||||
// List<NameValuePair> nameValuePairs = convertParams2NVPS(params);
|
||||
// httpMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, ENCODING));
|
||||
// }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,464 @@
|
||||
package com.ruoyi.hik.communicationCom;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLException;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
|
||||
//import org.apache.commons.httpclient.HttpStatus;
|
||||
//import org.apache.commons.httpclient.methods.PostMethod;
|
||||
//import org.apache.commons.httpclient.methods.PutMethod;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.Credentials;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLContextBuilder;
|
||||
import org.apache.http.conn.ssl.TrustStrategy;
|
||||
import org.apache.http.conn.ssl.X509HostnameVerifier;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
public class HttpsClientUtil {
|
||||
public HttpsClientUtil() throws Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static String strIP="";
|
||||
public static int iPort = 0;
|
||||
public static boolean bHttpsEnabled=false;
|
||||
|
||||
public static void httpsClientInit(String IP, String user, String Password)
|
||||
{
|
||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
Credentials credentials = new UsernamePasswordCredentials(user, Password);
|
||||
credentialsProvider.setCredentials(new AuthScope(IP, 443), credentials);
|
||||
HttpsClientUtil.httpsClient=HttpClients.custom().setSSLSocketFactory(HttpsClientUtil.createSSLConnSocketFactory()).setDefaultCredentialsProvider(credentialsProvider).build();
|
||||
}
|
||||
|
||||
public static SSLConnectionSocketFactory createSSLConnSocketFactory() {
|
||||
|
||||
SSLConnectionSocketFactory sslsf = null;
|
||||
try {
|
||||
|
||||
TrustStrategy trustStrategy=new TrustStrategy(){
|
||||
|
||||
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
|
||||
|
||||
X509HostnameVerifier x509HostnameVerifier=new X509HostnameVerifier(){
|
||||
|
||||
public boolean verify(String arg0, SSLSession arg1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void verify(String host, SSLSocket ssl) throws IOException {
|
||||
}
|
||||
|
||||
|
||||
public void verify(String host, X509Certificate cert) throws SSLException {
|
||||
}
|
||||
|
||||
public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
|
||||
}
|
||||
};
|
||||
|
||||
sslsf = new SSLConnectionSocketFactory(sslContext, x509HostnameVerifier);
|
||||
} catch (GeneralSecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sslsf;
|
||||
}
|
||||
|
||||
|
||||
public static CloseableHttpClient httpsClient=null;
|
||||
|
||||
public static String httpsGet(String url)
|
||||
{
|
||||
String Ret="";
|
||||
try {
|
||||
CloseableHttpResponse response = null;
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
|
||||
response = httpsClient.execute(httpGet);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
|
||||
} catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
}
|
||||
|
||||
public static String httpsPut(String url, String inboundInfo)
|
||||
{
|
||||
String Ret="";
|
||||
try {
|
||||
CloseableHttpResponse response = null;
|
||||
HttpPut httpPut = new HttpPut(url);
|
||||
|
||||
HttpEntity inboundInfoEntity = new StringEntity(inboundInfo, "UTF-8");
|
||||
httpPut.setEntity(inboundInfoEntity);
|
||||
|
||||
response = httpsClient.execute(httpPut);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
|
||||
} catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
}
|
||||
|
||||
public static String httpsPost(String url, String inboundInfo)
|
||||
{
|
||||
String Ret="";
|
||||
try {
|
||||
CloseableHttpResponse response = null;
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
HttpEntity inboundInfoEntity = new StringEntity(inboundInfo, "UTF-8");
|
||||
httpPost.setEntity(inboundInfoEntity);
|
||||
|
||||
response = httpsClient.execute(httpPost);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
|
||||
} catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
}
|
||||
|
||||
public static String httpsDelete(String url)
|
||||
{
|
||||
String Ret="";
|
||||
try {
|
||||
CloseableHttpResponse response = null;
|
||||
HttpDelete httpDelete = new HttpDelete(url);
|
||||
|
||||
response = httpsClient.execute(httpDelete);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
|
||||
} catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
}
|
||||
|
||||
public static String doPostStorageCloud(String url, String json,String faceimage,String boundary) throws Exception {
|
||||
|
||||
String Ret="";
|
||||
try{
|
||||
CloseableHttpResponse response = null;
|
||||
HttpPost method = new HttpPost(url);
|
||||
method.addHeader("Accept", "text/html, application/xhtml+xml");
|
||||
method.addHeader("Accept-Language", "zh-CN");
|
||||
method.addHeader("Content-Type","multipart/form-data; boundary=" + boundary);
|
||||
method.addHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
|
||||
method.addHeader("Accept-Encoding","gzip, deflate");
|
||||
method.addHeader("Connection","Keep-Alive");
|
||||
method.addHeader("Cache-Control","no-cache");
|
||||
// byte szTemp
|
||||
String bodyParam =
|
||||
"--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\"uploadStorageCloud\";\r\n"
|
||||
+ "Content-Type: text/json\r\n"
|
||||
+ "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n"
|
||||
+ json + "\r\n"
|
||||
+ "--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\"imageData\";\r\n"
|
||||
+ "Content-Type: image/jpeg\r\n"
|
||||
+ "Content-Length: " + Integer.toString(faceimage.length()) + "\r\n\r\n"
|
||||
+ faceimage
|
||||
+ "\r\n--" + boundary + "--\r\n";
|
||||
|
||||
|
||||
//HttpEntity inboundInfoEntity = new StringEntity(bodyParam, "UTF-8");
|
||||
HttpEntity inboundInfoEntity = new StringEntity(bodyParam);
|
||||
String strTemp = inboundInfoEntity.toString();
|
||||
|
||||
String strTemppp = strTemp;
|
||||
|
||||
method.setEntity(inboundInfoEntity);
|
||||
|
||||
response = httpsClient.execute(method);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
// Release the connection
|
||||
method.releaseConnection();
|
||||
|
||||
}catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
|
||||
}
|
||||
public static String doModFacePicRecord(String url, String json,String faceimage,String boundary) throws Exception {
|
||||
|
||||
String Ret="";
|
||||
try{
|
||||
CloseableHttpResponse response = null;
|
||||
HttpPost method = new HttpPost(url);
|
||||
method.addHeader("Accept", "text/html, application/xhtml+xml");
|
||||
method.addHeader("Accept-Language", "zh-CN");
|
||||
method.addHeader("Content-Type","multipart/form-data; boundary=" + boundary);
|
||||
method.addHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
|
||||
method.addHeader("Accept-Encoding","gzip, deflate");
|
||||
method.addHeader("Connection","Keep-Alive");
|
||||
method.addHeader("Cache-Control","no-cache");
|
||||
|
||||
String bodyParam =
|
||||
"--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n"
|
||||
+ "Content-Type: text/json\r\n"
|
||||
+ "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n"
|
||||
+ json + "\r\n"
|
||||
+ "--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\"FaceImage\";\r\n"
|
||||
+ "Content-Type: image/jpeg\r\n"
|
||||
+ "Content-Length: " + Integer.toString(faceimage.length()) + "\r\n\r\n"
|
||||
+ faceimage
|
||||
+ "\r\n--" + boundary + "--\r\n";
|
||||
|
||||
|
||||
HttpEntity inboundInfoEntity = new StringEntity(bodyParam, "UTF-8");
|
||||
String strTemp = inboundInfoEntity.toString();
|
||||
method.setEntity(inboundInfoEntity);
|
||||
|
||||
response = httpsClient.execute(method);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
// Release the connection
|
||||
method.releaseConnection();
|
||||
|
||||
}catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
|
||||
}
|
||||
public static String doPutWithType(String url, String inbound,String charset,String Type) throws Exception {
|
||||
|
||||
|
||||
|
||||
String Ret="";
|
||||
try {
|
||||
CloseableHttpResponse response = null;
|
||||
HttpPut httpPut = new HttpPut(url);
|
||||
|
||||
httpPut.addHeader("Content-Type", Type);
|
||||
HttpEntity inboundInfoEntity = new StringEntity(inbound, "UTF-8");
|
||||
httpPut.setEntity(inboundInfoEntity);
|
||||
|
||||
response = httpsClient.execute(httpPut);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
|
||||
} catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
}
|
||||
|
||||
public static String doPostwithBinaryData(String url, String json, String jsonName, String image, String imageName, String boundary) throws Exception {
|
||||
|
||||
String Ret="";
|
||||
try{
|
||||
CloseableHttpResponse response = null;
|
||||
HttpPost method = new HttpPost(url);
|
||||
method.addHeader("Accept", "text/html, application/xhtml+xml");
|
||||
method.addHeader("Accept-Language", "zh-CN");
|
||||
method.addHeader("Content-Type","multipart/form-data; boundary=" + boundary);
|
||||
method.addHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
|
||||
method.addHeader("Accept-Encoding","gzip, deflate");
|
||||
method.addHeader("Connection","Keep-Alive");
|
||||
method.addHeader("Cache-Control","no-cache");
|
||||
// byte szTemp
|
||||
String bodyParam =
|
||||
"--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\""+jsonName+"\";\r\n"
|
||||
+ "Content-Type: text/json\r\n"
|
||||
+ "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n"
|
||||
+ json + "\r\n"
|
||||
+ "--" + boundary + "\r\n"
|
||||
+ "Content-Disposition: form-data; name=\""+imageName+"\";\r\n"
|
||||
+ "Content-Type: image/jpeg\r\n"
|
||||
+ "Content-Length: " + Integer.toString(image.length()) + "\r\n\r\n"
|
||||
+ image
|
||||
+ "\r\n--" + boundary + "--\r\n";
|
||||
|
||||
|
||||
//HttpEntity inboundInfoEntity = new StringEntity(bodyParam, "UTF-8");
|
||||
HttpEntity inboundInfoEntity = new StringEntity(bodyParam);
|
||||
String strTemp = inboundInfoEntity.toString();
|
||||
|
||||
String strTemppp = strTemp;
|
||||
|
||||
method.setEntity(inboundInfoEntity);
|
||||
|
||||
response = httpsClient.execute(method);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.SC_OK)
|
||||
{
|
||||
Ret = "error "+statusCode;
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity == null)
|
||||
{
|
||||
Ret = "error response is null";
|
||||
}
|
||||
|
||||
Ret = EntityUtils.toString(entity, "utf-8");
|
||||
// Release the connection
|
||||
method.releaseConnection();
|
||||
|
||||
}catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Ret;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package com.ruoyi.hik.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 海康设备对象 hk_equipment
|
||||
*
|
||||
* @author zz
|
||||
* @date 2022-12-24
|
||||
*/
|
||||
@Data
|
||||
public class HikEquipmentInfo {
|
||||
|
||||
private static final long serialVersionUID=1L;
|
||||
|
||||
/**
|
||||
* 设备id
|
||||
*/
|
||||
private Long hkEquipmentId;
|
||||
/**
|
||||
* 设备类型0采集器1通道2门禁3考勤机
|
||||
*/
|
||||
private String equipmentType;
|
||||
|
||||
/**
|
||||
* 设备账号
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 设备密码
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 设备IP
|
||||
*/
|
||||
private String equipmentIp;
|
||||
/**
|
||||
* 设备端口号
|
||||
*/
|
||||
private Short equipmentPort;
|
||||
|
||||
|
||||
/**
|
||||
* 用户句柄:登录后的操作句柄
|
||||
*/
|
||||
public int lUserID = -1;
|
||||
/**
|
||||
* 布防句柄:布防后的操作句柄
|
||||
*/
|
||||
public int lAlarmHandle = -1;
|
||||
|
||||
/**
|
||||
* 监听句柄:监听后的操作句柄
|
||||
*/
|
||||
public int lListenHandle = -1;
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.ruoyi.hik.domain;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class HikSecurity {
|
||||
private HikSecurityUserCheck userCheck;
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package com.ruoyi.hik.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class HikSecurityUserCheck {
|
||||
// <!--ro, req, enum, 校验结果状态, subType:int, [200#200,401#401]-->200
|
||||
private Integer statusValue;
|
||||
// <!--ro, opt, enum, 校验结果状态字符信息, subType:string, [OK#OK,Unauthorized#Unauthorized]-->OK
|
||||
private String statusString;
|
||||
// <!--ro, opt, bool, 是否是默认密码-->true
|
||||
private Boolean isDefaultPassword;
|
||||
// <!--ro, opt, bool, 是否是风险密码-->true
|
||||
private Boolean isRiskPassword;
|
||||
// <!--ro, opt, bool, 是否已激活-->true
|
||||
private Boolean isActivated;
|
||||
// <!--ro, opt, int, 密码剩余有效天数, desc:返回负值,表示密码已经超期使用,例如"-3表示密码已经超期使用3天-->1
|
||||
private Integer residualValidity;
|
||||
// <!--ro, opt, enum, 锁定状态, subType:string, [unlock#未锁定,locked#已锁定]-->unlock
|
||||
private String lockStatus;
|
||||
// <!--ro, opt, int, 解锁剩余时间, unit:s, unitType:时间-->1
|
||||
private Integer unlockTime;
|
||||
// <!--ro, opt, int, 重试次数-->1
|
||||
private Integer retryLoginTime;
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package com.ruoyi.hik.domain;
|
||||
|
||||
/**
|
||||
* http请求返回对象
|
||||
*
|
||||
* @author xuzhou
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class HttpResult {
|
||||
|
||||
/**
|
||||
* 状态码
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 返回数据
|
||||
*/
|
||||
private String stringEntity;
|
||||
|
||||
public HttpResult() {
|
||||
}
|
||||
|
||||
/**
|
||||
* http请求返回对象
|
||||
*
|
||||
* @param status 返回状态
|
||||
* @param stringEntity 返回数据
|
||||
*/
|
||||
public HttpResult(Integer status, String stringEntity) {
|
||||
this.status = status;
|
||||
this.stringEntity = stringEntity;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getStringEntity() {
|
||||
return stringEntity;
|
||||
}
|
||||
|
||||
public void setStringEntity(String stringEntity) {
|
||||
this.stringEntity = stringEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HttpResult{" +
|
||||
"status=" + status +
|
||||
", stringEntity='" + stringEntity + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
|
||||
Loading…
Reference in New Issue
Block a user