Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
clx-performance
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
姜武杰
clx-performance
Commits
52f2d055
提交
52f2d055
authored
10月 17, 2024
作者:
姜武杰
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加日志看是否可以正常进入方法
上级
c931a947
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
156 行删除
+46
-156
HttpsUtils.java
...ce-web/src/main/java/com/openapi/sdk/util/HttpsUtils.java
+46
-156
没有找到文件。
clx-performance-web/src/main/java/com/openapi/sdk/util/HttpsUtils.java
浏览文件 @
52f2d055
package
com
.
openapi
.
sdk
.
util
;
package
com
.
openapi
.
sdk
.
util
;
import
javax.net.ssl.*
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.DataOutputStream
;
import
org.apache.http.HttpEntity
;
import
java.io.IOException
;
import
org.apache.http.HttpResponse
;
import
java.io.InputStreamReader
;
import
org.apache.http.client.config.RequestConfig
;
import
java.net.HttpURLConnection
;
import
org.apache.http.client.methods.HttpPost
;
import
java.net.URL
;
import
org.apache.http.entity.StringEntity
;
import
java.security.SecureRandom
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
java.security.cert.CertificateException
;
import
org.apache.http.impl.client.HttpClients
;
import
java.security.cert.X509Certificate
;
import
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
;
import
org.apache.http.util.EntityUtils
;
import
java.nio.charset.StandardCharsets
;
@Slf4j
public
class
HttpsUtils
{
public
class
HttpsUtils
{
public
HttpsUtils
()
{
private
static
final
int
MAX_TOTAL
=
800
;
private
static
final
int
MAX_PER_ROUTE
=
20
;
private
static
final
int
CONNECT_TIMEOUT
=
5000
;
private
static
final
int
READ_TIMEOUT
=
5000
;
private
static
final
CloseableHttpClient
httpClient
;
static
{
PoolingHttpClientConnectionManager
connManager
=
new
PoolingHttpClientConnectionManager
();
connManager
.
setMaxTotal
(
MAX_TOTAL
);
connManager
.
setDefaultMaxPerRoute
(
MAX_PER_ROUTE
);
httpClient
=
HttpClients
.
custom
()
.
setConnectionManager
(
connManager
)
.
build
();
}
}
public
static
String
doPost
(
String
url
,
int
connentTimeout
,
int
readTimeout
)
throws
Exception
{
public
static
String
doPost
(
String
url
,
int
connTimeout
,
int
readTimeout
)
throws
Exception
{
HttpURLConnection
conn
=
null
;
return
doPost
(
url
,
""
,
connTimeout
,
readTimeout
);
InputStreamReader
isReader
=
null
;
StringBuffer
result
=
new
StringBuffer
();
try
{
trustAllHttpsCertificates
();
HostnameVerifier
hv
=
new
HostnameVerifier
()
{
public
boolean
verify
(
String
urlHostName
,
SSLSession
session
)
{
System
.
out
.
println
(
"Warning: URL Host: "
+
urlHostName
+
" vs. "
+
session
.
getPeerHost
());
return
true
;
}
};
HttpsURLConnection
.
setDefaultHostnameVerifier
(
hv
);
conn
=
(
HttpURLConnection
)(
new
URL
(
url
)).
openConnection
();
conn
.
setDoInput
(
true
);
conn
.
setDoOutput
(
true
);
conn
.
setUseCaches
(
false
);
conn
.
setRequestMethod
(
"POST"
);
conn
.
setConnectTimeout
(
connentTimeout
);
conn
.
setReadTimeout
(
readTimeout
);
isReader
=
new
InputStreamReader
(
conn
.
getInputStream
(),
"UTF-8"
);
char
[]
bfchar
=
new
char
[
2048
];
int
length
=
0
;
while
((
length
=
isReader
.
read
(
bfchar
))
!=
-
1
)
{
String
temp
=
new
String
(
bfchar
,
0
,
length
);
result
.
append
(
temp
);
}
}
catch
(
Exception
var17
)
{
System
.
err
.
println
(
"发送 POST 请求出现异常!"
+
var17
.
getMessage
());
throw
var17
;
}
finally
{
try
{
if
(
isReader
!=
null
)
{
isReader
.
close
();
}
}
catch
(
IOException
var16
)
{
System
.
err
.
println
(
"关闭数据流出错了!\n"
+
var16
.
getMessage
()
+
"\n"
);
throw
var16
;
}
}
return
result
.
toString
();
}
}
public
static
String
doPost
(
String
url
,
String
param
,
int
readTimeout
,
int
connectTimeout
)
throws
Exception
{
public
static
String
doPost
(
String
url
,
String
param
,
int
readTimeout
,
int
connectTimeout
)
throws
Exception
{
InputStreamReader
isReader
=
null
;
log
.
info
(
"====>进入自定义HttpsUtils doPost"
);
HttpURLConnection
conn
=
null
;
DataOutputStream
dos
=
null
;
StringBuffer
result
=
new
StringBuffer
();
try
{
trustAllHttpsCertificates
();
HostnameVerifier
hv
=
new
HostnameVerifier
()
{
public
boolean
verify
(
String
urlHostName
,
SSLSession
session
)
{
System
.
out
.
println
(
"Warning: URL Host: "
+
urlHostName
+
" vs. "
+
session
.
getPeerHost
());
return
true
;
}
};
HttpsURLConnection
.
setDefaultHostnameVerifier
(
hv
);
conn
=
(
HttpURLConnection
)(
new
URL
(
url
)).
openConnection
();
conn
.
setDoInput
(
true
);
conn
.
setDoOutput
(
true
);
conn
.
setUseCaches
(
false
);
conn
.
setRequestProperty
(
"Content-Length"
,
String
.
valueOf
(
param
.
getBytes
(
"UTF-8"
).
length
));
conn
.
setRequestMethod
(
"POST"
);
conn
.
setConnectTimeout
(
connectTimeout
);
conn
.
setRequestProperty
(
"charset"
,
"UTF-8"
);
conn
.
setReadTimeout
(
readTimeout
);
conn
.
connect
();
dos
=
new
DataOutputStream
(
conn
.
getOutputStream
());
int
length
=
0
;
int
totalLength
=
param
.
length
();
while
(
length
<
totalLength
)
{
int
endLength
=
length
+
1024
;
if
(
endLength
>
totalLength
)
{
endLength
=
totalLength
;
}
dos
.
write
(
param
.
substring
(
length
,
endLength
).
getBytes
(
"UTF-8"
));
length
=
endLength
;
dos
.
flush
();
}
dos
.
close
();
isReader
=
new
InputStreamReader
(
conn
.
getInputStream
(),
"UTF-8"
);
char
[]
bfchar
=
new
char
[
2048
];
int
rlength
=
0
;
while
((
rlength
=
isReader
.
read
(
bfchar
))
!=
-
1
)
{
String
temp
=
new
String
(
bfchar
,
0
,
rlength
);
result
.
append
(
temp
);
}
return
result
.
toString
();
}
catch
(
Exception
var21
)
{
System
.
err
.
println
(
"发送 POST 请求出现异常!"
+
var21
.
getMessage
()
+
"e:"
+
var21
);
throw
var21
;
}
finally
{
try
{
if
(
isReader
!=
null
)
{
isReader
.
close
();
}
}
catch
(
IOException
var20
)
{
System
.
err
.
println
(
"关闭数据流出错了!\n"
+
var20
.
getMessage
()
+
"\n"
);
throw
var20
;
}
try
{
if
(
conn
!=
null
)
{
conn
.
disconnect
();
}
}
catch
(
Exception
var19
)
{
System
.
err
.
println
(
"关闭连接出错了!\n"
+
var19
.
getMessage
()
+
"\n"
);
throw
var19
;
}
}
}
private
static
void
trustAllHttpsCertificates
()
throws
Exception
{
TrustManager
[]
trustAllCerts
=
new
TrustManager
[
1
];
TrustManager
tm
=
new
miTM
();
trustAllCerts
[
0
]
=
tm
;
SSLContext
sc
=
SSLContext
.
getInstance
(
"SSL"
);
sc
.
init
((
KeyManager
[])
null
,
trustAllCerts
,
(
SecureRandom
)
null
);
HttpsURLConnection
.
setDefaultSSLSocketFactory
(
sc
.
getSocketFactory
());
}
static
class
miTM
implements
TrustManager
,
X509TrustManager
{
miTM
()
{
}
public
X509Certificate
[]
getAcceptedIssuers
()
{
RequestConfig
requestConfig
=
RequestConfig
.
custom
()
return
null
;
.
setConnectTimeout
(
connectTimeout
)
}
.
setSocketTimeout
(
readTimeout
)
.
build
();
public
boolean
isServerTrusted
(
X509Certificate
[]
certs
)
{
return
true
;
}
public
boolean
isClientTrusted
(
X509Certificate
[]
certs
)
{
HttpPost
httpPost
=
new
HttpPost
(
url
);
return
true
;
httpPost
.
setConfig
(
requestConfig
);
}
httpPost
.
setHeader
(
"Content-Type"
,
"application/json; charset=utf-8"
);
httpPost
.
setEntity
(
new
StringEntity
(
param
,
StandardCharsets
.
UTF_8
));
public
void
checkServerTrusted
(
X509Certificate
[]
certs
,
String
authType
)
throws
CertificateException
{
}
public
void
checkClientTrusted
(
X509Certificate
[]
certs
,
String
authType
)
throws
CertificateException
{
HttpResponse
response
=
httpClient
.
execute
(
httpPost
);
HttpEntity
entity
=
response
.
getEntity
();
if
(
entity
!=
null
)
{
return
EntityUtils
.
toString
(
entity
,
StandardCharsets
.
UTF_8
);
}
}
return
""
;
}
}
}
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论