Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
common
Commits
1edc5eed
Commit
1edc5eed
authored
Aug 14, 2024
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
11
parent
ec76a699
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
129 additions
and
97 deletions
+129
-97
HttpsUtil.java
src/main/java/com/egolm/common/HttpsUtil.java
+129
-97
No files found.
src/main/java/com/egolm/common/HttpsUtil.java
View file @
1edc5eed
...
@@ -12,7 +12,12 @@ import java.net.HttpURLConnection;
...
@@ -12,7 +12,12 @@ import java.net.HttpURLConnection;
import
java.net.Proxy
;
import
java.net.Proxy
;
import
java.net.URL
;
import
java.net.URL
;
import
java.nio.charset.Charset
;
import
java.nio.charset.Charset
;
import
java.security.KeyManagementException
;
import
java.security.KeyStore
;
import
java.security.KeyStore
;
import
java.security.KeyStoreException
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.cert.CertificateException
;
import
java.security.cert.X509Certificate
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
...
@@ -29,15 +34,16 @@ import org.apache.http.client.config.RequestConfig;
...
@@ -29,15 +34,16 @@ import org.apache.http.client.config.RequestConfig;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.conn.ssl.NoopHostnameVerifier
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.ssl.SSLContexts
;
import
org.apache.http.ssl.SSLContexts
;
import
org.apache.http.ssl.TrustStrategy
;
import
org.apache.http.util.EntityUtils
;
import
org.apache.http.util.EntityUtils
;
import
com.egolm.common.exception.HttpRequestException
;
import
com.egolm.common.exception.HttpRequestException
;
/**
/**
*
*
* @author 曲欣亮
* @author 曲欣亮
...
@@ -46,84 +52,113 @@ import com.egolm.common.exception.HttpRequestException;
...
@@ -46,84 +52,113 @@ import com.egolm.common.exception.HttpRequestException;
*/
*/
public
class
HttpsUtil
{
public
class
HttpsUtil
{
private
static
RequestConfig
requestConfig
;
private
static
RequestConfig
requestConfig
;
static
{
static
{
RequestConfig
.
Builder
configBuilder
=
RequestConfig
.
custom
();
RequestConfig
.
Builder
configBuilder
=
RequestConfig
.
custom
();
// 设置连接超时
// 设置连接超时
configBuilder
.
setConnectTimeout
(
10
*
1000
);
configBuilder
.
setConnectTimeout
(
10
*
1000
);
// 设置读取超时
// 设置读取超时
configBuilder
.
setSocketTimeout
(
10
*
10000
);
configBuilder
.
setSocketTimeout
(
10
*
10000
);
requestConfig
=
configBuilder
.
build
();
requestConfig
=
configBuilder
.
build
();
}
}
/**
public
static
CloseableHttpClient
trustAll
()
{
* 发送无参数的GET请求
// 配置,发送https请求时,忽略ssl证书认证(否则会报错没有证书)
*/
SSLContext
sslContext
=
null
;
public
static
String
doGet
(
String
url
)
{
try
{
return
doGet
(
url
,
null
);
sslContext
=
SSLContexts
.
custom
().
loadTrustMaterial
(
null
,
new
TrustStrategy
()
{
}
@Override
public
boolean
isTrusted
(
X509Certificate
[]
x509Certificates
,
String
s
)
throws
CertificateException
{
/**
return
true
;
* 发送有参数的GET请求,参数为MAP key-value格式
}
*/
}).
build
();
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
params
)
{
}
catch
(
NoSuchAlgorithmException
e
)
{
List
<
String
>
paramList
=
new
ArrayList
<
String
>();
e
.
printStackTrace
();
if
(
params
!=
null
){
}
catch
(
KeyManagementException
e
)
{
for
(
String
key
:
params
.
keySet
())
{
e
.
printStackTrace
();
paramList
.
add
(
key
+
"="
+
params
.
get
(
key
));
}
catch
(
KeyStoreException
e
)
{
}
e
.
printStackTrace
();
if
(
paramList
.
size
()>
0
){
}
if
(
url
.
indexOf
(
"?"
)==-
1
){
url
=
url
+
"?"
+
StringUtil
.
join
(
"&"
,
paramList
);
//创建httpClient
}
else
{
CloseableHttpClient
client
=
HttpClients
.
custom
().
setSSLContext
(
sslContext
)
url
=
url
+
"&"
+
StringUtil
.
join
(
"&"
,
paramList
);
.
setSSLHostnameVerifier
(
new
NoopHostnameVerifier
()).
build
();
}
}
return
client
;
}
}
System
.
out
.
println
(
"doGet--"
+
url
);
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
/**
HttpGet
httpGet
=
new
HttpGet
(
url
.
replaceAll
(
" "
,
"%20"
));
* 发送无参数的GET请求
httpGet
.
setConfig
(
requestConfig
);
*/
CloseableHttpResponse
response
=
null
;
public
static
String
doGet
(
String
url
)
{
HttpEntity
entity
=
null
;
return
doGet
(
url
,
null
);
try
{
}
response
=
httpclient
.
execute
(
httpGet
);
entity
=
response
.
getEntity
();
/**
String
result
=
EntityUtils
.
toString
(
entity
,
"UTF-8"
);
* 发送有参数的GET请求,参数为MAP key-value格式
*/
return
result
;
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
params
)
{
}
catch
(
Exception
e
)
{
List
<
String
>
paramList
=
new
ArrayList
<
String
>();
throw
new
HttpRequestException
(
e
);
if
(
params
!=
null
)
{
}
finally
{
for
(
String
key
:
params
.
keySet
())
{
try
{
paramList
.
add
(
key
+
"="
+
params
.
get
(
key
));
if
(
entity
!=
null
){
}
EntityUtils
.
consume
(
entity
);
if
(
paramList
.
size
()
>
0
)
{
}
if
(
url
.
indexOf
(
"?"
)
==
-
1
)
{
url
=
url
+
"?"
+
StringUtil
.
join
(
"&"
,
paramList
);
}
else
{
url
=
url
+
"&"
+
StringUtil
.
join
(
"&"
,
paramList
);
}
}
}
System
.
out
.
println
(
"doGet--"
+
url
);
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
HttpGet
httpGet
=
new
HttpGet
(
url
.
replaceAll
(
" "
,
"%20"
));
httpGet
.
setConfig
(
requestConfig
);
CloseableHttpResponse
response
=
null
;
HttpEntity
entity
=
null
;
try
{
response
=
httpclient
.
execute
(
httpGet
);
entity
=
response
.
getEntity
();
String
result
=
EntityUtils
.
toString
(
entity
,
"UTF-8"
);
return
result
;
}
catch
(
Exception
e
)
{
throw
new
HttpRequestException
(
e
);
}
finally
{
try
{
if
(
entity
!=
null
)
{
EntityUtils
.
consume
(
entity
);
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
try
{
try
{
if
(
response
!=
null
)
{
if
(
response
!=
null
)
{
response
.
close
();
response
.
close
();
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
try
{
try
{
httpclient
.
close
();
httpclient
.
close
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
}
}
public
static
String
get
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
header
,
Proxy
proxy
,
SSLSocketFactory
sslSocketFactory
)
throws
HttpRequestException
{
public
static
String
get
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
header
,
Proxy
proxy
,
SSLSocketFactory
sslSocketFactory
)
throws
HttpRequestException
{
HttpsURLConnection
connection
=
null
;
HttpsURLConnection
connection
=
null
;
try
{
try
{
String
requestBody
=
HttpUtil
.
toQueryString
(
parameters
,
"utf-8"
);
String
requestBody
=
HttpUtil
.
toQueryString
(
parameters
,
"utf-8"
);
requestUrl
=
requestUrl
+
(
requestUrl
.
contains
(
"?"
)
?
(
requestUrl
.
endsWith
(
"&"
)
?
""
:
"&"
)
:
"?"
)
+
requestBody
;
requestUrl
=
requestUrl
+
(
requestUrl
.
contains
(
"?"
)
?
(
requestUrl
.
endsWith
(
"&"
)
?
""
:
"&"
)
:
"?"
)
+
requestBody
;
URL
GET_URL
=
new
URL
(
requestUrl
);
URL
GET_URL
=
new
URL
(
requestUrl
);
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
GET_URL
.
openConnection
()
:
GET_URL
.
openConnection
(
proxy
));
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
GET_URL
.
openConnection
()
if
(
sslSocketFactory
!=
null
)
{
:
GET_URL
.
openConnection
(
proxy
));
if
(
sslSocketFactory
!=
null
)
{
connection
.
setSSLSocketFactory
(
sslSocketFactory
);
connection
.
setSSLSocketFactory
(
sslSocketFactory
);
}
}
connection
.
setRequestMethod
(
"GET"
);
connection
.
setRequestMethod
(
"GET"
);
...
@@ -142,22 +177,25 @@ public class HttpsUtil {
...
@@ -142,22 +177,25 @@ public class HttpsUtil {
connection
.
disconnect
();
connection
.
disconnect
();
}
}
}
}
public
static
String
post
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
headers
,
SSLSocketFactory
sslSocketFactory
,
Proxy
proxy
)
throws
HttpRequestException
{
public
static
String
post
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
headers
,
System
.
out
.
println
(
"----"
+
HttpUtil
.
toQueryString
(
parameters
));
SSLSocketFactory
sslSocketFactory
,
Proxy
proxy
)
throws
HttpRequestException
{
System
.
out
.
println
(
"----"
+
HttpUtil
.
toQueryString
(
parameters
));
return
HttpsUtil
.
post
(
requestUrl
,
HttpUtil
.
toQueryString
(
parameters
),
headers
,
sslSocketFactory
,
proxy
);
return
HttpsUtil
.
post
(
requestUrl
,
HttpUtil
.
toQueryString
(
parameters
),
headers
,
sslSocketFactory
,
proxy
);
}
}
public
static
String
post
(
String
requestUrl
,
String
text
,
Map
<
String
,
String
>
headers
,
SSLSocketFactory
sslSocketFactory
,
Proxy
proxy
)
throws
HttpRequestException
{
public
static
String
post
(
String
requestUrl
,
String
text
,
Map
<
String
,
String
>
headers
,
SSLSocketFactory
sslSocketFactory
,
Proxy
proxy
)
throws
HttpRequestException
{
HttpsURLConnection
connection
=
null
;
HttpsURLConnection
connection
=
null
;
try
{
try
{
byte
[]
bytes
=
text
==
null
?
new
byte
[
0
]
:
text
.
getBytes
();
byte
[]
bytes
=
text
==
null
?
new
byte
[
0
]
:
text
.
getBytes
();
URL
POST_URL
=
new
URL
(
requestUrl
);
URL
POST_URL
=
new
URL
(
requestUrl
);
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
POST_URL
.
openConnection
()
:
POST_URL
.
openConnection
(
proxy
));
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
POST_URL
.
openConnection
()
if
(
sslSocketFactory
!=
null
)
{
:
POST_URL
.
openConnection
(
proxy
));
if
(
sslSocketFactory
!=
null
)
{
connection
.
setSSLSocketFactory
(
sslSocketFactory
);
connection
.
setSSLSocketFactory
(
sslSocketFactory
);
}
}
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setDoInput
(
true
);
connection
.
setRequestMethod
(
"POST"
);
connection
.
setRequestMethod
(
"POST"
);
...
@@ -181,7 +219,7 @@ public class HttpsUtil {
...
@@ -181,7 +219,7 @@ public class HttpsUtil {
connection
.
disconnect
();
connection
.
disconnect
();
}
}
}
}
/**
/**
*
*
* <p>
* <p>
...
@@ -196,7 +234,7 @@ public class HttpsUtil {
...
@@ -196,7 +234,7 @@ public class HttpsUtil {
* @return
* @return
*/
*/
public
static
String
doPostForXml
(
String
url
,
String
xml
)
{
public
static
String
doPostForXml
(
String
url
,
String
xml
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
CloseableHttpClient
httpClient
=
trustAll
();
//
HttpClients.createDefault();
HttpPost
httpPost
=
new
HttpPost
(
url
);
HttpPost
httpPost
=
new
HttpPost
(
url
);
httpPost
.
setConfig
(
requestConfig
);
httpPost
.
setConfig
(
requestConfig
);
CloseableHttpResponse
response
=
null
;
CloseableHttpResponse
response
=
null
;
...
@@ -236,35 +274,31 @@ public class HttpsUtil {
...
@@ -236,35 +274,31 @@ public class HttpsUtil {
}
}
}
}
}
}
public
static
String
doPost
(
String
url
)
{
public
static
String
doPost
(
String
url
)
{
String
result
=
HttpUtil
.
post
(
url
);
String
result
=
HttpUtil
.
post
(
url
);
return
result
;
return
result
;
}
}
/**
/**
* 发送有参数的POST请求,参数为json格式
* 发送有参数的POST请求,参数为json格式
*/
*/
public
static
String
doPostForJson
(
String
url
,
String
json
)
{
public
static
String
doPostForJson
(
String
url
,
String
json
)
{
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
String
result
=
HttpUtil
.
post
(
url
,
json
,
map
,
null
);
String
result
=
HttpUtil
.
post
(
url
,
json
,
map
,
null
);
return
result
;
return
result
;
}
}
public
static
String
doPostForJson
(
String
url
,
String
json
,
Map
<
String
,
String
>
headers
)
{
public
static
String
doPostForJson
(
String
url
,
String
json
,
Map
<
String
,
String
>
headers
)
{
String
result
=
HttpUtil
.
post
(
url
,
json
,
headers
,
null
);
String
result
=
HttpUtil
.
post
(
url
,
json
,
headers
,
null
);
return
result
;
return
result
;
}
}
/**
/**
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
*
*
* @param url
* @param url 请求地址 form表单url地址
* 请求地址 form表单url地址
* @param filePath 文件在服务器保存路径
* @param filePath
* 文件在服务器保存路径
* @return String url的响应信息返回值
* @return String url的响应信息返回值
* @throws IOException
* @throws IOException
*/
*/
...
@@ -307,7 +341,7 @@ public class HttpsUtil {
...
@@ -307,7 +341,7 @@ public class HttpsUtil {
sb
.
append
(
"--"
);
// 必须多两道线
sb
.
append
(
"--"
);
// 必须多两道线
sb
.
append
(
BOUNDARY
);
sb
.
append
(
BOUNDARY
);
sb
.
append
(
"\r\n"
);
sb
.
append
(
"\r\n"
);
sb
.
append
(
"Content-Disposition: form-data;name=\"file\";filename=\""
+
file
.
getName
()
+
"\"\r\n"
);
sb
.
append
(
"Content-Disposition: form-data;name=\"file\";filename=\""
+
file
.
getName
()
+
"\"\r\n"
);
sb
.
append
(
"Content-Type:application/octet-stream\r\n\r\n"
);
sb
.
append
(
"Content-Type:application/octet-stream\r\n\r\n"
);
byte
[]
head
=
sb
.
toString
().
getBytes
(
"utf-8"
);
byte
[]
head
=
sb
.
toString
().
getBytes
(
"utf-8"
);
...
@@ -357,17 +391,15 @@ public class HttpsUtil {
...
@@ -357,17 +391,15 @@ public class HttpsUtil {
reader
.
close
();
reader
.
close
();
}
}
}
}
return
result
;
return
result
;
}
}
/**
/**
* 调用证书进行退款 WX提供
* 调用证书进行退款 WX提供
*
*
* @param servicePartner
* @param servicePartner 商户号
* 商户号
* @param sslPath 证书地址 "D:/10016225.p12"
* @param sslPath
* 证书地址 "D:/10016225.p12"
* @throws Exception
* @throws Exception
*/
*/
public
static
String
doPostBySSL
(
String
servicePartner
,
String
sslPath
,
String
xml
,
String
requestUrl
)
public
static
String
doPostBySSL
(
String
servicePartner
,
String
sslPath
,
String
xml
,
String
requestUrl
)
...
@@ -393,7 +425,7 @@ public class HttpsUtil {
...
@@ -393,7 +425,7 @@ public class HttpsUtil {
out
.
close
();
out
.
close
();
return
responseBody
(
connection
);
return
responseBody
(
connection
);
}
}
public
static
String
responseBody
(
HttpURLConnection
connection
)
throws
Exception
{
public
static
String
responseBody
(
HttpURLConnection
connection
)
throws
Exception
{
byte
[]
bytes
;
byte
[]
bytes
;
try
{
try
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment