Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
孙克
/
smf-core
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 93b61fb9
由
LN
编写于
2022-04-08 10:07:35 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
增加hik接口key和secret的配置
1 个父辈
da33cb54
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
517 行增加
和
478 行删除
src/main/java/com/neotel/smfcore/hikvision/HikApi.java
src/main/java/com/neotel/smfcore/hikvision/HikApiCache.java
src/main/java/com/neotel/smfcore/hikvision/HikConfig.java
src/main/java/com/neotel/smfcore/hikvision/bean/api/HikApiRequest.java
src/main/java/com/neotel/smfcore/hikvision/util/HttpHelper.java
src/main/resources/config/application.yml
src/main/java/com/neotel/smfcore/hikvision/HikApi.java
查看文件 @
93b61fb
...
...
@@ -68,6 +68,9 @@ public class HikApi {
return
apiAddr
;
}
if
(
ObjectUtil
.
isNotEmpty
(
config
.
mesServer
)){
return
config
.
mesServer
;
}
// if(ObjectUtil.isNotEmpty(mesServer)&&ObjectUtil.isNotEmpty(apiName)){
// if(mesServer.endsWith("/")){
// return mesServer+apiName;
...
...
@@ -109,11 +112,11 @@ public class HikApi {
String
data
=
JsonUtil
.
toJsonStr
(
dataMap
);
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
data
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
config
.
key_1_transferOrderApi
,
config
.
secret_1_transferOrderApi
,
info
,
new
Date
()
);
log
.
info
(
"Hik 转储单接口 (transferOrder): 发送"
+
data
);
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -157,11 +160,11 @@ public class HikApi {
String
data
=
JsonUtil
.
toJsonStr
(
dataMap
);
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
data
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
2
,
url
,
config
.
key_2_transferOrderInApi
,
config
.
secret_2_transferOrderInApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
data
);
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -215,11 +218,11 @@ public class HikApi {
String
data
=
JsonUtil
.
toJsonStr
(
dataMap
);
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
data
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
3
,
url
,
config
.
key_3_scheduleTaskApi
,
config
.
secret_3_scheduleTaskApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
data
);
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -259,11 +262,11 @@ public class HikApi {
// String data = JsonUtil.toJsonStr(dataMap);
// data String 41 是 料盘ID
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
trayId
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
4
,
url
,
config
.
key_4_returnMaterialApi
,
config
.
secret_4_returnMaterialApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
info
.
getData
());
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -334,11 +337,11 @@ public class HikApi {
String
data
=
JsonUtil
.
toJsonStr
(
dataMap
);
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
data
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
5
,
url
,
config
.
key_5_putInApi
,
config
.
secret_5_putInApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
data
);
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -393,11 +396,11 @@ public class HikApi {
String
data
=
JsonUtil
.
toJsonStr
(
dataMap
);
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
data
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
6
,
url
,
config
.
key_6_checkOutApi
,
config
.
secret_6_checkOutApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
data
);
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -432,11 +435,11 @@ public class HikApi {
String
url
=
getUrl
(
config
.
addr_7_updateOrderApi
,
"updateOrderApi"
);
// data String 16 是 工单号/合单号
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
orderNo
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
6
,
url
,
config
.
key_7_updateOrderApi
,
config
.
secret_7_updateOrderApi
,
info
,
new
Date
()
);
List
<
Object
>
resultList
=
new
ArrayList
<>();
log
.
info
(
apiName
+
" 发送"
+
info
.
getData
());
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -503,11 +506,11 @@ public class HikApi {
String
data
=
JsonUtil
.
toJsonStr
(
dataMap
);
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
data
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
8
,
url
,
config
.
key_8_riCheckApi
,
config
.
secret_8_riCheckApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
info
.
getData
());
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -541,11 +544,11 @@ public class HikApi {
List
<
Object
>
resultList
=
new
ArrayList
<>();
// data String 41 是 补料单号
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
replenishmentNo
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
9
,
url
,
config
.
key_9_updateRepOrderApi
,
config
.
secret_9_updateRepOrderApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
info
.
getData
());
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -600,11 +603,11 @@ public class HikApi {
String
data
=
JsonUtil
.
toJsonStr
(
dataMap
);
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
data
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
1
0
,
url
,
config
.
key_10_reservedOrderApi
,
config
.
secret_10_reservedOrderApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
info
.
getData
());
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
@@ -650,11 +653,11 @@ public class HikApi {
String
url
=
getUrl
(
config
.
addr_11_orderEndApi
,
"orderEndApi"
);
// data String 41 是 补料单号
RequestParam
info
=
new
RequestParam
(
getReqCode
(),
userName
,
orderNo
);
HikApiRequest
request
=
new
HikApiRequest
(
1
,
url
,
new
Date
(),
info
);
HikApiRequest
request
=
new
HikApiRequest
(
1
1
,
url
,
config
.
key_11_orderEndApi
,
config
.
secret_11_orderEndApi
,
info
,
new
Date
()
);
log
.
info
(
apiName
+
" 发送"
+
info
.
getData
());
boolean
needResend
=
false
;
try
{
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
url
,
info
);
ResponseParam
responseInfo
=
HttpHelper
.
postJson
(
request
);
if
(
responseInfo
==
null
||
responseInfo
.
getCode
().
equals
(-
1
))
{
// needResend = true;
...
...
src/main/java/com/neotel/smfcore/hikvision/HikApiCache.java
查看文件 @
93b61fb
...
...
@@ -89,7 +89,7 @@ public class HikApiCache {
});
for
(
HikApiRequest
apiRequest
:
failedList
)
{
ResponseParam
responseParam
=
HttpHelper
.
postJson
(
apiRequest
.
getUrl
(),
apiRequest
.
getParam
()
);
ResponseParam
responseParam
=
HttpHelper
.
postJson
(
apiRequest
);
boolean
needResend
=
false
;
if
(
responseParam
==
null
||
responseParam
.
getCode
().
equals
(-
1
))
{
...
...
src/main/java/com/neotel/smfcore/hikvision/HikConfig.java
查看文件 @
93b61fb
...
...
@@ -54,6 +54,99 @@ public class HikConfig {
@Value
(
"${hik.addr_11_orderEndApi:}"
)
public
String
addr_11_orderEndApi
=
""
;
// key_1_transferOrderApi:
// key_2_transferOrderInApi:
// key_3_scheduleTaskApi:
// key_4_returnMaterialApi:
// key_5_putInApi:
// key_6_checkOutApi:
// key_7_updateOrderApi:
// key_8_riCheckApi:
// key_9_updateRepOrderApi:
// key_10_reservedOrderApi:
// key_11_orderEndApi:
@Value
(
"${hik.key_1_transferOrderApi:}"
)
public
String
key_1_transferOrderApi
=
""
;
@Value
(
"${hik.key_2_transferOrderInApi:}"
)
public
String
key_2_transferOrderInApi
=
""
;
@Value
(
"${hik.key_3_scheduleTaskApi:}"
)
public
String
key_3_scheduleTaskApi
=
""
;
@Value
(
"${hik.key_4_returnMaterialApi:}"
)
public
String
key_4_returnMaterialApi
=
""
;
@Value
(
"${hik.key_5_putInApi:}"
)
public
String
key_5_putInApi
=
""
;
@Value
(
"${hik.key_6_checkOutApi:}"
)
public
String
key_6_checkOutApi
=
""
;
@Value
(
"${hik.key_7_updateOrderApi:}"
)
public
String
key_7_updateOrderApi
=
""
;
@Value
(
"${hik.key_8_riCheckApi:}"
)
public
String
key_8_riCheckApi
=
""
;
@Value
(
"${hik.key_9_updateRepOrderApi:}"
)
public
String
key_9_updateRepOrderApi
=
""
;
@Value
(
"${hik.key_10_reservedOrderApi:}"
)
public
String
key_10_reservedOrderApi
=
""
;
@Value
(
"${hik.key_11_orderEndApi:}"
)
public
String
key_11_orderEndApi
=
""
;
// secret_1_transferOrderApi:
// secret_2_transferOrderInApi:
// secret_3_scheduleTaskApi:
// secret_4_returnMaterialApi:
// secret_5_putInApi:
// secret_6_checkOutApi:
// secret_7_updateOrderApi:
// secret_8_riCheckApi:
// secret_9_updateRepOrderApi:
// secret_10_reservedOrderApi:
// secret_11_orderEndApi:
@Value
(
"${hik.secret_1_transferOrderApi:}"
)
public
String
secret_1_transferOrderApi
=
""
;
@Value
(
"${hik.secret_2_transferOrderInApi:}"
)
public
String
secret_2_transferOrderInApi
=
""
;
@Value
(
"${hik.secret_3_scheduleTaskApi:}"
)
public
String
secret_3_scheduleTaskApi
=
""
;
@Value
(
"${hik.secret_4_returnMaterialApi:}"
)
public
String
secret_4_returnMaterialApi
=
""
;
@Value
(
"${hik.secret_5_putInApi:}"
)
public
String
secret_5_putInApi
=
""
;
@Value
(
"${hik.secret_6_checkOutApi:}"
)
public
String
secret_6_checkOutApi
=
""
;
@Value
(
"${hik.secret_7_updateOrderApi:}"
)
public
String
secret_7_updateOrderApi
=
""
;
@Value
(
"${hik.secret_8_riCheckApi:}"
)
public
String
secret_8_riCheckApi
=
""
;
@Value
(
"${hik.secret_9_updateRepOrderApi:}"
)
public
String
secret_9_updateRepOrderApi
=
""
;
@Value
(
"${hik.secret_10_reservedOrderApi:}"
)
public
String
secret_10_reservedOrderApi
=
""
;
@Value
(
"${hik.secret_11_orderEndApi:}"
)
public
String
secret_11_orderEndApi
=
""
;
@Autowired
private
DataCache
dataCache
;
...
...
@@ -80,100 +173,4 @@ public class HikConfig {
}
// //读取配置中的地址
// private static String mesServer = "";
// @Value("${hik.server:}")
// public void setMesServer(String mesServer) {
// HikApi.mesServer = mesServer;
// }
//
// private static String baseCode="";
// @Value("${hik.baseCode:}")
// public void setBaseCode(String baseCode) {
// HikApi.baseCode = baseCode;
// }
//
// private static String lgort="";
// @Value("${hik.lgort:}")
// public void setLgort(String lgort) {
// HikApi.lgort = lgort;
// }
//
// private static String addr_1_transferOrderApi ="";
// @Value("${hik.addr_1_transferOrderApi:}")
// public void setTransferOrderApi(String transferOrderApi) {
// HikApi.addr_1_transferOrderApi = transferOrderApi;
// }
//
// private static String addr_2_transferOrderInApi="";
// @Value("${hik.addr_2_transferOrderInApi:}")
// public void setTransferOrderInApi(String transferOrderInApi) {
// HikApi.addr_2_transferOrderInApi = transferOrderInApi;
// }
//
// private static String addr_3_scheduleTaskApi="";
// @Value("${hik.addr_3_scheduleTaskApi:}")
// public void setScheduleTaskApi(String scheduleTaskApi) {
// HikApi.addr_3_scheduleTaskApi = scheduleTaskApi;
// }
//
// private static String addr_4_returnMaterialApi="";
// @Value("${hik.addr_4_returnMaterialApi:}")
// public void setReturnMaterialApi(String returnMaterialApi) {
// HikApi.addr_4_returnMaterialApi = returnMaterialApi;
// }
//
// private static String addr_5_putInApi="";
// @Value("${hik.addr_5_putInApi:}")
// public void setPutInApi(String putInApi) {
// HikApi.addr_5_putInApi = putInApi;
// }
//
// private static String addr_6_checkOutApi="";
// @Value("${hik.addr_6_checkOutApi:}")
// public void setCheckOutApi(String checkOutApi) {
// HikApi.addr_6_checkOutApi = checkOutApi;
// }
//
// private static String addr_7_updateOrderApi="";
// @Value("${hik.addr_7_updateOrderApi:}")
// public void setUpdateOrderApi(String updateOrderApi) {
// HikApi.addr_7_updateOrderApi = updateOrderApi;
// }
//
// private static String addr_8_riCheckApi="";
// @Value("${hik.addr_8_riCheckApi:}")
// public void setRiCheckApi(String riCheckApi) {
// HikApi.addr_8_riCheckApi = riCheckApi;
// }
//
// private static String addr_9_updateRepOrderApi="";
// @Value("${hik.addr_9_updateRepOrderApi:}")
// public void setUpdateRepOrderApi(String updateRepOrderApi) {
// HikApi.addr_9_updateRepOrderApi = updateRepOrderApi;
// }
//
// private static String addr_10_reservedOrderApi="";
// @Value("${hik.addr_10_reservedOrderApi:}")
// public void setReservedOrderApi(String reservedOrderApi) {
// HikApi.addr_10_reservedOrderApi = reservedOrderApi;
// }
//
// private static String addr_11_orderEndApi="";
// @Value("${hik.addr_11_orderEndApi:}")
// public void setOrderEndApi(String orderEndApi) {
// HikApi.addr_11_orderEndApi = orderEndApi;
// }
// @Autowired
// private DataCache dataCache;
// @PostConstruct
// public void init(){
// mesServer=dataCache.GetConfigCache("hik.server","hik.server",mesServer);
// baseCode=dataCache.GetConfigCache("hik.baseCode","hik.baseCode",baseCode);
// lgort=dataCache.GetConfigCache("hik.lgort","hik.lgort",lgort);
// log.info("HIK服务器地址:"+mesServer+",baseCode="+baseCode+",lgort="+lgort);
// }
}
src/main/java/com/neotel/smfcore/hikvision/bean/api/HikApiRequest.java
查看文件 @
93b61fb
...
...
@@ -22,13 +22,22 @@ public class HikApiRequest implements Serializable {
private
String
url
=
""
;
/**
*
消息创建时间
*
接口key
*/
private
Date
createTime
;
private
String
key
=
""
;
/**
* 接口密钥
*/
private
String
secret
=
""
;
/**
* 参数信息
*/
private
RequestParam
param
;
/**
* 消息创建时间
*/
private
Date
createTime
;
}
src/main/java/com/neotel/smfcore/hikvision/util/HttpHelper.java
查看文件 @
93b61fb
...
...
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.neotel.smfcore.common.exception.ApiException
;
import
com.neotel.smfcore.common.utils.DateUtil
;
import
com.neotel.smfcore.hikvision.bean.api.HikApiRequest
;
import
com.neotel.smfcore.hikvision.bean.api.RequestParam
;
import
com.neotel.smfcore.hikvision.bean.api.ResponseParam
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -45,274 +46,363 @@ public class HttpHelper {
// return postJson(url,params,null, "http");
// }
public
static
String
get
(
String
url
,
HashMap
<
String
,
String
>
params
)
throws
ApiException
{
return
get
(
url
,
params
,
null
,
"http"
);
}
public
static
String
postParamWithAuth
(
String
url
,
Map
<
String
,
Object
>
paramMap
,
String
user
,
String
pwd
)
throws
ApiException
{
PrintWriter
out
=
null
;
BufferedReader
in
=
null
;
String
result
=
""
;
try
{
URL
realUrl
=
new
URL
(
url
);
// 打开和URL之间的连接
URLConnection
conn
=
realUrl
.
openConnection
();
// 设置通用的请求属性
conn
.
setRequestProperty
(
"accept"
,
"*/*"
);
conn
.
setRequestProperty
(
"connection"
,
"Keep-Alive"
);
conn
.
setRequestProperty
(
"user-agent"
,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"
);
conn
.
setRequestProperty
(
"Accept-Charset"
,
"UTF-8"
);
//设置用户名和密码
if
(
Strings
.
isNotBlank
(
user
)){
String
auth
=
user
+
":"
+
pwd
;
//对其进行加密
byte
[]
rel
=
Base64
.
getEncoder
().
encode
(
auth
.
getBytes
());
String
res
=
new
String
(
rel
);
//设置认证属性
conn
.
setRequestProperty
(
"Authorization"
,
"Basic "
+
res
);
}
// conn.setRequestProperty("Charset", "UTF-8");
// 发送POST请求必须设置如下两行
conn
.
setDoOutput
(
true
);
conn
.
setDoInput
(
true
);
conn
.
setConnectTimeout
(
20000
);
// 获取URLConnection对象对应的输出流
//out = new PrintWriter(conn.getOutputStream());
out
=
new
PrintWriter
(
new
OutputStreamWriter
(
conn
.
getOutputStream
(),
"utf-8"
));
// 设置请求属性
String
param
=
""
;
if
(
paramMap
!=
null
&&
paramMap
.
size
()
>
0
)
{
Iterator
<
String
>
ite
=
paramMap
.
keySet
().
iterator
();
while
(
ite
.
hasNext
())
{
String
key
=
ite
.
next
();
// key
Object
valueObj
=
paramMap
.
get
(
key
);
String
value
=
""
;
if
(
valueObj
!=
null
){
if
(
valueObj
instanceof
Date
){
DateUtil
.
toDateString
((
Date
)
valueObj
,
"yyyyMMdd"
);
}
else
{
value
=
valueObj
.
toString
();
}
}
param
+=
key
+
"="
+
value
+
"&"
;
}
param
=
param
.
substring
(
0
,
param
.
length
()
-
1
);
}
// 发送请求参数
out
.
print
(
param
);
// flush输出流的缓冲
out
.
flush
();
// 定义BufferedReader输入流来读取URL的响应
//in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
in
=
new
BufferedReader
(
new
InputStreamReader
(
conn
.
getInputStream
(),
"utf-8"
));
String
line
;
while
((
line
=
in
.
readLine
())
!=
null
)
{
result
+=
line
;
}
}
catch
(
Exception
e
)
{
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
// 使用finally块来关闭输出流、输入流
finally
{
try
{
if
(
out
!=
null
)
{
out
.
close
();
}
if
(
in
!=
null
)
{
in
.
close
();
}
}
catch
(
IOException
ex
)
{
throw
new
ApiException
(
"Request ["
+
url
+
"] close instream failed:"
+
ex
.
getMessage
());
}
}
return
result
;
}
/**
* 向指定URL发送POST请求
* @param url
* @param paramMap
* @return 响应结果
*/
public
static
String
postParam
(
String
url
,
Map
<
String
,
Object
>
paramMap
)
throws
ApiException
{
return
postParamWithAuth
(
url
,
paramMap
,
null
,
null
);
}
/**
* 发送POST请求
*
* @param url
* 请求URL地址
* @param params
* 请求参数
* @param protocol
* 请求协议 "http" / "https"
* @return 服务器响应的请求结果
*/
private
static
String
postInBody
(
String
url
,
HashMap
<
String
,
String
>
params
,
HashMap
<
String
,
String
>
cookies
,
String
protocol
)
throws
ApiException
{
// if (protocol.equalsIgnoreCase("https")) {
// Protocol httpsProtocol = new Protocol("https", new SecureProtocolSocketFactoryImpl(), 443);
// Protocol.registerProtocol("https", httpsProtocol);
// }
HttpClient
httpClient
=
new
HttpClient
();
PostMethod
postMethod
=
new
PostMethod
(
url
);
// 设置请求参数
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
NameValuePair
[]
data
=
new
NameValuePair
[
params
.
size
()];
Iterator
<
Map
.
Entry
<
String
,
String
>>
iterator
=
params
.
entrySet
().
iterator
();
int
i
=
0
;
while
(
iterator
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
iterator
.
next
();
data
[
i
]
=
new
NameValuePair
(
entry
.
getKey
(),
entry
.
getValue
());
++
i
;
}
postMethod
.
setRequestBody
(
data
);
}
// 设置cookie
if
(
cookies
!=
null
&&
!
cookies
.
isEmpty
())
{
Iterator
<
Map
.
Entry
<
String
,
String
>>
iterator
=
cookies
.
entrySet
().
iterator
();
StringBuilder
buffer
=
new
StringBuilder
(
128
);
while
(
iterator
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
iterator
.
next
();
buffer
.
append
(
entry
.
getKey
()).
append
(
"="
).
append
(
entry
.
getValue
()).
append
(
"; "
);
}
// 设置cookie策略
postMethod
.
getParams
().
setCookiePolicy
(
CookiePolicy
.
IGNORE_COOKIES
);
// 设置cookie内容
postMethod
.
setRequestHeader
(
"Cookie"
,
buffer
.
toString
());
}
// 设置User-Agent
postMethod
.
setRequestHeader
(
"User-Agent"
,
USER_AGENT
);
// 设置建立连接超时时间
httpClient
.
getHttpConnectionManager
().
getParams
().
setConnectionTimeout
(
CONNECTION_TIMEOUT
);
// 设置读数据超时时间
httpClient
.
getHttpConnectionManager
().
getParams
().
setSoTimeout
(
READ_DATA_TIMEOUT
);
// 设置编码
postMethod
.
getParams
().
setParameter
(
HttpMethodParams
.
HTTP_CONTENT_CHARSET
,
CONTENT_CHARSET
);
// 使用系统提供的默认的恢复策略
postMethod
.
getParams
().
setParameter
(
HttpMethodParams
.
RETRY_HANDLER
,
new
DefaultHttpMethodRetryHandler
());
try
{
try
{
int
statusCode
=
httpClient
.
executeMethod
(
postMethod
);
if
(
statusCode
!=
HttpStatus
.
SC_OK
)
{
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
postMethod
.
getStatusLine
());
}
// 读取内容
byte
[]
responseBody
=
postMethod
.
getResponseBody
();
return
new
String
(
responseBody
,
CONTENT_CHARSET
);
}
finally
{
// 释放链接
postMethod
.
releaseConnection
();
}
}
catch
(
HttpException
e
)
{
// 发生致命的异常,可能是协议不对或者返回的内容有问题
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
catch
(
IOException
e
)
{
// 发生网络异常
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
}
/**
* 发送Get请求
*
* @param url
* 请求URL地址
* @param params
* 请求参数
* @param protocol
* 请求协议 "http" / "https"
* @return 服务器响应的请求结果
*/
public
static
String
get
(
String
url
,
HashMap
<
String
,
String
>
params
,
HashMap
<
String
,
String
>
cookies
,
String
protocol
)
throws
ApiException
{
// if (protocol.equalsIgnoreCase("https")) {
// Protocol httpsProtocol = new Protocol("https", new SecureProtocolSocketFactoryImpl(), 443);
// Protocol.registerProtocol("https", httpsProtocol);
// }
HttpClient
httpClient
=
new
HttpClient
();
GetMethod
getMethod
=
new
GetMethod
(
url
);
// PostMethod postMethod = new PostMethod(url);
// 设置请求参数
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
NameValuePair
[]
data
=
new
NameValuePair
[
params
.
size
()];
Iterator
<
Map
.
Entry
<
String
,
String
>>
iterator
=
params
.
entrySet
().
iterator
();
int
i
=
0
;
while
(
iterator
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
iterator
.
next
();
data
[
i
]
=
new
NameValuePair
(
entry
.
getKey
(),
entry
.
getValue
());
++
i
;
}
getMethod
.
setQueryString
(
data
);
}
// 设置cookie
if
(
cookies
!=
null
&&
!
cookies
.
isEmpty
())
{
Iterator
<
Map
.
Entry
<
String
,
String
>>
iterator
=
cookies
.
entrySet
().
iterator
();
StringBuilder
buffer
=
new
StringBuilder
(
128
);
while
(
iterator
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
iterator
.
next
();
buffer
.
append
(
entry
.
getKey
()).
append
(
"="
).
append
(
entry
.
getValue
()).
append
(
"; "
);
}
// 设置cookie策略
getMethod
.
getParams
().
setCookiePolicy
(
CookiePolicy
.
IGNORE_COOKIES
);
// 设置cookie内容
getMethod
.
setRequestHeader
(
"Cookie"
,
buffer
.
toString
());
}
// public static String get(String url,HashMap<String, String> params) throws ApiException {
// return get(url,params,null, "http");
// }
//
// public static String postParamWithAuth(String url, Map<String, Object> paramMap, String user, String pwd) throws ApiException {
// PrintWriter out = null;
// BufferedReader in = null;
// String result = "";
// try {
// URL realUrl = new URL(url);
// // 打开和URL之间的连接
// URLConnection conn = realUrl.openConnection();
// // 设置通用的请求属性
// conn.setRequestProperty("accept", "*/*");
// conn.setRequestProperty("connection", "Keep-Alive");
// conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// conn.setRequestProperty("Accept-Charset", "UTF-8");
//
// //设置用户名和密码
//
// if(Strings.isNotBlank(user)){
// String auth = user+":"+pwd;
// //对其进行加密
// byte[] rel = Base64.getEncoder().encode(auth.getBytes());
// String res = new String(rel);
// //设置认证属性
// conn.setRequestProperty("Authorization","Basic " + res);
// }
//
// // conn.setRequestProperty("Charset", "UTF-8");
// // 发送POST请求必须设置如下两行
// conn.setDoOutput(true);
// conn.setDoInput(true);
// conn.setConnectTimeout(20000);
// // 获取URLConnection对象对应的输出流
// //out = new PrintWriter(conn.getOutputStream());
// out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));
//
// // 设置请求属性
// String param = "";
// if (paramMap != null && paramMap.size() > 0) {
// Iterator<String> ite = paramMap.keySet().iterator();
// while (ite.hasNext()) {
// String key = ite.next();// key
// Object valueObj = paramMap.get(key);
// String value = "";
// if(valueObj != null){
// if(valueObj instanceof Date){
// DateUtil.toDateString((Date)valueObj,"yyyyMMdd");
// }else{
// value = valueObj.toString();
// }
// }
// param += key + "=" + value + "&";
// }
// param = param.substring(0, param.length() - 1);
// }
//
// // 发送请求参数
// out.print(param);
// // flush输出流的缓冲
// out.flush();
// // 定义BufferedReader输入流来读取URL的响应
// //in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//
// in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
// String line;
// while ((line = in.readLine()) != null) {
// result += line;
// }
// } catch (Exception e) {
// throw new ApiException("Request [" + url + "] failed:" + e.getMessage());
// }
// // 使用finally块来关闭输出流、输入流
// finally {
// try {
// if (out != null) {
// out.close();
// }
// if (in != null) {
// in.close();
// }
// } catch (IOException ex) {
// throw new ApiException("Request [" + url + "] close instream failed:" + ex.getMessage());
// }
// }
// return result;
// }
//
//
// /**
// * 向指定URL发送POST请求
// * @param url
// * @param paramMap
// * @return 响应结果
// */
// public static String postParam(String url, Map<String, Object> paramMap) throws ApiException {
// return postParamWithAuth(url,paramMap, null, null);
// }
//
// /**
// * 发送POST请求
// *
// * @param url
// * 请求URL地址
// * @param params
// * 请求参数
// * @param protocol
// * 请求协议 "http" / "https"
// * @return 服务器响应的请求结果
// */
// private static String postInBody(String url, HashMap<String, String> params,
// HashMap<String, String> cookies, String protocol) throws ApiException {
//// if (protocol.equalsIgnoreCase("https")) {
//// Protocol httpsProtocol = new Protocol("https", new SecureProtocolSocketFactoryImpl(), 443);
//// Protocol.registerProtocol("https", httpsProtocol);
//// }
//
// HttpClient httpClient = new HttpClient();
// PostMethod postMethod = new PostMethod(url);
//
// // 设置请求参数
// if (params != null && !params.isEmpty()) {
// NameValuePair[] data = new NameValuePair[params.size()];
// Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
// int i = 0;
// while (iterator.hasNext()) {
// Map.Entry<String, String> entry = iterator.next();
// data[i] = new NameValuePair(entry.getKey(), entry.getValue());
// ++i;
// }
// postMethod.setRequestBody(data);
// }
//
// // 设置cookie
// if (cookies != null && !cookies.isEmpty()) {
// Iterator<Map.Entry<String, String>> iterator = cookies.entrySet().iterator();
// StringBuilder buffer = new StringBuilder(128);
// while (iterator.hasNext()) {
// Map.Entry<String, String> entry = iterator.next();
// buffer.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
// }
// // 设置cookie策略
// postMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
// // 设置cookie内容
// postMethod.setRequestHeader("Cookie", buffer.toString());
// }
//
// // 设置User-Agent
// postMethod.setRequestHeader("User-Agent", USER_AGENT);
// // 设置建立连接超时时间
// httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
// // 设置读数据超时时间
// httpClient.getHttpConnectionManager().getParams().setSoTimeout(READ_DATA_TIMEOUT);
// // 设置编码
// postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
// // 使用系统提供的默认的恢复策略
// postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//
// try {
// try {
// int statusCode = httpClient.executeMethod(postMethod);
// if (statusCode != HttpStatus.SC_OK) {
// throw new ApiException("Request [" + url + "] failed:" + postMethod.getStatusLine());
// }
// // 读取内容
// byte[] responseBody = postMethod.getResponseBody();
// return new String(responseBody, CONTENT_CHARSET);
// } finally {
// // 释放链接
// postMethod.releaseConnection();
// }
// } catch (HttpException e) {
// // 发生致命的异常,可能是协议不对或者返回的内容有问题
// throw new ApiException("Request [" + url + "] failed:" + e.getMessage());
// } catch (IOException e) {
// // 发生网络异常
// throw new ApiException("Request [" + url + "] failed:" + e.getMessage());
// }
// }
//
// /**
// * 发送Get请求
// *
// * @param url
// * 请求URL地址
// * @param params
// * 请求参数
// * @param protocol
// * 请求协议 "http" / "https"
// * @return 服务器响应的请求结果
// */
// public static String get(String url, HashMap<String, String> params,
// HashMap<String, String> cookies, String protocol) throws ApiException {
//// if (protocol.equalsIgnoreCase("https")) {
//// Protocol httpsProtocol = new Protocol("https", new SecureProtocolSocketFactoryImpl(), 443);
//// Protocol.registerProtocol("https", httpsProtocol);
//// }
//
// HttpClient httpClient = new HttpClient();
// GetMethod getMethod = new GetMethod(url);
//// PostMethod postMethod = new PostMethod(url);
//
// // 设置请求参数
// if (params != null && !params.isEmpty()) {
// NameValuePair[] data = new NameValuePair[params.size()];
// Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
// int i = 0;
// while (iterator.hasNext()) {
// Map.Entry<String, String> entry = iterator.next();
// data[i] = new NameValuePair(entry.getKey(), entry.getValue());
// ++i;
// }
// getMethod.setQueryString(data);
// }
//
// // 设置cookie
// if (cookies != null && !cookies.isEmpty()) {
// Iterator<Map.Entry<String, String>> iterator = cookies.entrySet().iterator();
// StringBuilder buffer = new StringBuilder(128);
// while (iterator.hasNext()) {
// Map.Entry<String, String> entry = iterator.next();
// buffer.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
// }
// // 设置cookie策略
// getMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
// // 设置cookie内容
// getMethod.setRequestHeader("Cookie", buffer.toString());
// }
//
// // 设置User-Agent
// getMethod.setRequestHeader("User-Agent", USER_AGENT);
// // 设置建立连接超时时间
// httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
// // 设置读数据超时时间
// httpClient.getHttpConnectionManager().getParams().setSoTimeout(READ_DATA_TIMEOUT);
// // 设置编码
// getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
// // 使用系统提供的默认的恢复策略
// getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//
// try {
// try {
// int statusCode = httpClient.executeMethod(getMethod);
// if (statusCode != HttpStatus.SC_OK) {
// throw new ApiException("Request [" + url + "] failed:" + getMethod.getStatusLine());
// }
// // 读取内容
// byte[] responseBody = getMethod.getResponseBody();
// return new String(responseBody, CONTENT_CHARSET);
// } finally {
// // 释放链接
// getMethod.releaseConnection();
// }
// } catch (HttpException e) {
// // 发生致命的异常,可能是协议不对或者返回的内容有问题
// throw new ApiException("Request [" + url + "] failed:" + e.getMessage());
// } catch (IOException e) {
// // 发生网络异常
// throw new ApiException("Request [" + url + "] failed:" + e.getMessage());
// }
// }
// /**
// * 发送POST请求(上传文件)
// *
// * @param url
// * 请求URL地址
// * @param params
// * 请求参数
// * @param protocol
// * 请求协议 "http" / "https"
// * @param fp
// * 上传的文件
// * @return 服务器响应的请求结果
// */
// private static String postWithFile(String url,
// HashMap<String, String> params, HashMap<String, String> cookies,
// FilePart fp, String protocol) throws ApiException {
//// if (protocol.equalsIgnoreCase("https")) {
//// Protocol httpsProtocol = new Protocol("https", new SecureProtocolSocketFactoryImpl(), 443);
//// Protocol.registerProtocol("https", httpsProtocol);
//// }
//
// HttpClient httpClient = new HttpClient();
// PostMethod postMethod = new PostMethod(url);
//
// List<Part> parts = new ArrayList<Part>();
// parts.add(fp);
//
// // 设置请求参数
// if (params != null && !params.isEmpty()) {
// Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
// while (iterator.hasNext()) {
// Map.Entry<String, String> entry = iterator.next();
// // 设置参数为UTF-8编码集,解决中文乱码问题,并且将参数加到post中
// parts.add(new StringPart(entry.getKey(), entry.getValue(), CONTENT_CHARSET));
// }
// }
// MultipartRequestEntity requestEntity = new MultipartRequestEntity(
// (Part[]) parts.toArray(), postMethod.getParams());
// postMethod.setRequestEntity(requestEntity);
//
// // 设置cookie
// if (cookies != null && !cookies.isEmpty()) {
// Iterator<Map.Entry<String, String>> iterator = cookies.entrySet().iterator();
// StringBuilder buffer = new StringBuilder(128);
// while (iterator.hasNext()) {
// Map.Entry<String, String> entry = iterator.next();
// buffer.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
// }
// // 设置cookie策略
// postMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
// // 设置cookie内容
// postMethod.setRequestHeader("Cookie", buffer.toString());
// }
//
// // 设置User-Agent
// postMethod.setRequestHeader("User-Agent", USER_AGENT);
// // 设置建立连接超时时间
// httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
// // 设置读数据超时时间
// httpClient.getHttpConnectionManager().getParams().setSoTimeout(READ_DATA_TIMEOUT);
// // 使用系统提供的默认的恢复策略
// postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//
// // 发送请求
// try {
// try {
// postMethod.getParams().setContentCharset("UTF-8");
// int statusCode = httpClient.executeMethod(postMethod);
// if (statusCode != HttpStatus.SC_OK) {
// throw new ApiException("Request [" + url + "] failed:" + postMethod.getStatusLine());
// }
// // 读取内容
// byte[] responseBody = postMethod.getResponseBody();
// return new String(responseBody, CONTENT_CHARSET);
// } finally {
// // 释放链接
// postMethod.releaseConnection();
// }
// } catch (HttpException e) {
// // 发生致命的异常,可能是协议不对或者返回的内容有问题
// throw new ApiException("Request [" + url + "] failed:" + e.getMessage());
// } catch (IOException e) {
// // 发生网络异常
// throw new ApiException("Request [" + url + "] failed:" + e.getMessage());
// }
// }
// 设置User-Agent
getMethod
.
setRequestHeader
(
"User-Agent"
,
USER_AGENT
);
// 设置建立连接超时时间
httpClient
.
getHttpConnectionManager
().
getParams
().
setConnectionTimeout
(
CONNECTION_TIMEOUT
);
// 设置读数据超时时间
httpClient
.
getHttpConnectionManager
().
getParams
().
setSoTimeout
(
READ_DATA_TIMEOUT
);
// 设置编码
getMethod
.
getParams
().
setParameter
(
HttpMethodParams
.
HTTP_CONTENT_CHARSET
,
CONTENT_CHARSET
);
// 使用系统提供的默认的恢复策略
getMethod
.
getParams
().
setParameter
(
HttpMethodParams
.
RETRY_HANDLER
,
new
DefaultHttpMethodRetryHandler
());
try
{
try
{
int
statusCode
=
httpClient
.
executeMethod
(
getMethod
);
if
(
statusCode
!=
HttpStatus
.
SC_OK
)
{
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
getMethod
.
getStatusLine
());
}
// 读取内容
byte
[]
responseBody
=
getMethod
.
getResponseBody
();
return
new
String
(
responseBody
,
CONTENT_CHARSET
);
}
finally
{
// 释放链接
getMethod
.
releaseConnection
();
}
}
catch
(
HttpException
e
)
{
// 发生致命的异常,可能是协议不对或者返回的内容有问题
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
catch
(
IOException
e
)
{
// 发生网络异常
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
}
/**
* POST提交JSON格式的内容
*/
private
static
String
postJson
(
String
url
,
Map
<
String
,
Object
>
params
,
HashMap
<
String
,
String
>
cookies
,
String
protocol
)
throws
ApiException
{
HashMap
<
String
,
String
>
cookies
,
String
protocol
,
String
key
,
String
secret
)
throws
ApiException
{
// if (protocol.equalsIgnoreCase("https")) {
// Protocol httpsProtocol = new Protocol("https", new SecureProtocolSocketFactoryImpl(), 443);
// Protocol.registerProtocol("https", httpsProtocol);
...
...
@@ -382,110 +472,27 @@ public class HttpHelper {
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
}
/**
* 发送POST请求(上传文件)
*
* @param url
* 请求URL地址
* @param params
* 请求参数
* @param protocol
* 请求协议 "http" / "https"
* @param fp
* 上传的文件
* @return 服务器响应的请求结果
*/
private
static
String
postWithFile
(
String
url
,
HashMap
<
String
,
String
>
params
,
HashMap
<
String
,
String
>
cookies
,
FilePart
fp
,
String
protocol
)
throws
ApiException
{
// if (protocol.equalsIgnoreCase("https")) {
// Protocol httpsProtocol = new Protocol("https", new SecureProtocolSocketFactoryImpl(), 443);
// Protocol.registerProtocol("https", httpsProtocol);
// }
HttpClient
httpClient
=
new
HttpClient
();
PostMethod
postMethod
=
new
PostMethod
(
url
);
List
<
Part
>
parts
=
new
ArrayList
<
Part
>();
parts
.
add
(
fp
);
// 设置请求参数
if
(
params
!=
null
&&
!
params
.
isEmpty
())
{
Iterator
<
Map
.
Entry
<
String
,
String
>>
iterator
=
params
.
entrySet
().
iterator
();
while
(
iterator
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
iterator
.
next
();
// 设置参数为UTF-8编码集,解决中文乱码问题,并且将参数加到post中
parts
.
add
(
new
StringPart
(
entry
.
getKey
(),
entry
.
getValue
(),
CONTENT_CHARSET
));
}
}
MultipartRequestEntity
requestEntity
=
new
MultipartRequestEntity
(
(
Part
[])
parts
.
toArray
(),
postMethod
.
getParams
());
postMethod
.
setRequestEntity
(
requestEntity
);
// 设置cookie
if
(
cookies
!=
null
&&
!
cookies
.
isEmpty
())
{
Iterator
<
Map
.
Entry
<
String
,
String
>>
iterator
=
cookies
.
entrySet
().
iterator
();
StringBuilder
buffer
=
new
StringBuilder
(
128
);
while
(
iterator
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
iterator
.
next
();
buffer
.
append
(
entry
.
getKey
()).
append
(
"="
).
append
(
entry
.
getValue
()).
append
(
"; "
);
}
// 设置cookie策略
postMethod
.
getParams
().
setCookiePolicy
(
CookiePolicy
.
IGNORE_COOKIES
);
// 设置cookie内容
postMethod
.
setRequestHeader
(
"Cookie"
,
buffer
.
toString
());
}
// 设置User-Agent
postMethod
.
setRequestHeader
(
"User-Agent"
,
USER_AGENT
);
// 设置建立连接超时时间
httpClient
.
getHttpConnectionManager
().
getParams
().
setConnectionTimeout
(
CONNECTION_TIMEOUT
);
// 设置读数据超时时间
httpClient
.
getHttpConnectionManager
().
getParams
().
setSoTimeout
(
READ_DATA_TIMEOUT
);
// 使用系统提供的默认的恢复策略
postMethod
.
getParams
().
setParameter
(
HttpMethodParams
.
RETRY_HANDLER
,
new
DefaultHttpMethodRetryHandler
());
// 发送请求
try
{
try
{
postMethod
.
getParams
().
setContentCharset
(
"UTF-8"
);
int
statusCode
=
httpClient
.
executeMethod
(
postMethod
);
if
(
statusCode
!=
HttpStatus
.
SC_OK
)
{
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
postMethod
.
getStatusLine
());
}
// 读取内容
byte
[]
responseBody
=
postMethod
.
getResponseBody
();
return
new
String
(
responseBody
,
CONTENT_CHARSET
);
}
finally
{
// 释放链接
postMethod
.
releaseConnection
();
}
}
catch
(
HttpException
e
)
{
// 发生致命的异常,可能是协议不对或者返回的内容有问题
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
catch
(
IOException
e
)
{
// 发生网络异常
throw
new
ApiException
(
"Request ["
+
url
+
"] failed:"
+
e
.
getMessage
());
}
}
public
static
ResponseParam
postJson
(
String
url
,
RequestParam
requestInfo
)
throws
ApiException
{
public
static
ResponseParam
postJson
(
HikApiRequest
request
)
throws
ApiException
{
String
url
=
request
.
getUrl
();
String
key
=
request
.
getKey
();
String
secret
=
request
.
getSecret
();
RequestParam
param
=
request
.
getParam
();
if
(
ObjectUtil
.
isEmpty
(
url
)){
return
null
;
}
Map
<
String
,
Object
>
params
=
new
HashMap
<>();
HashMap
<
String
,
String
>
cookies
=
null
;
String
protocol
=
"http"
;
params
.
put
(
"reqCode"
,
requestInfo
.
getReqCode
());
params
.
put
(
"userCode"
,
requestInfo
.
getUserCode
());
params
.
put
(
"data"
,
requestInfo
.
getData
());
String
result
=
postJson
(
url
,
params
,
cookies
,
protocol
);
params
.
put
(
"reqCode"
,
param
.
getReqCode
());
params
.
put
(
"userCode"
,
param
.
getUserCode
());
params
.
put
(
"data"
,
param
.
getData
());
String
result
=
postJson
(
url
,
params
,
cookies
,
protocol
,
key
,
secret
);
ResponseParam
responseInfo
=
JsonUtil
.
toObj
(
result
,
ResponseParam
.
class
);
if
(
responseInfo
==
null
)
{
log
.
error
(
"["
+
url
+
"]
发送["
+
requestInfo
.
getReqCode
()
+
"]["
+
requestInfo
.
getUserCode
()
+
"]["
+
requestInfo
.
getData
()
+
"],返回值:"
+
result
);
responseInfo
=
new
ResponseParam
(
requestInfo
.
getReqCode
(),
"-1"
,
""
,
""
);
log
.
error
(
"["
+
url
+
"]
["
+
key
+
"]["
+
secret
+
"]发送["
+
param
.
getReqCode
()
+
"]["
+
param
.
getUserCode
()
+
"]["
+
param
.
getData
()
+
"],返回值:"
+
result
);
responseInfo
=
new
ResponseParam
(
param
.
getReqCode
(),
"-1"
,
""
,
""
);
}
else
{
log
.
info
(
"["
+
url
+
"]
发送["
+
requestInfo
.
getReqCode
()
+
"]["
+
requestInfo
.
getUserCode
()
+
"]["
+
requestInfo
.
getData
()
+
"],返回值:"
+
result
);
log
.
info
(
"["
+
url
+
"]
["
+
key
+
"]["
+
secret
+
"]发送["
+
param
.
getReqCode
()
+
"]["
+
param
.
getUserCode
()
+
"]["
+
param
.
getData
()
+
"],返回值:"
+
result
);
}
return
responseInfo
;
...
...
src/main/resources/config/application.yml
查看文件 @
93b61fb
...
...
@@ -15,6 +15,29 @@ hik:
addr_9_updateRepOrderApi
:
addr_10_reservedOrderApi
:
addr_11_orderEndApi
:
key_1_transferOrderApi
:
key_2_transferOrderInApi
:
key_3_scheduleTaskApi
:
key_4_returnMaterialApi
:
key_5_putInApi
:
key_6_checkOutApi
:
key_7_updateOrderApi
:
key_8_riCheckApi
:
key_9_updateRepOrderApi
:
key_10_reservedOrderApi
:
key_11_orderEndApi
:
secret_1_transferOrderApi
:
secret_2_transferOrderInApi
:
secret_3_scheduleTaskApi
:
secret_4_returnMaterialApi
:
secret_5_putInApi
:
secret_6_checkOutApi
:
secret_7_updateOrderApi
:
secret_8_riCheckApi
:
secret_9_updateRepOrderApi
:
secret_10_reservedOrderApi
:
secret_11_orderEndApi
:
# 文件存储路径
file
:
...
...
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论