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
Show 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;
import
java.net.Proxy
;
import
java.net.URL
;
import
java.nio.charset.Charset
;
import
java.security.KeyManagementException
;
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.HashMap
;
import
java.util.List
;
...
...
@@ -29,15 +34,16 @@ 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.conn.ssl.NoopHostnameVerifier
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.ssl.SSLContexts
;
import
org.apache.http.ssl.TrustStrategy
;
import
org.apache.http.util.EntityUtils
;
import
com.egolm.common.exception.HttpRequestException
;
/**
*
* @author 曲欣亮
...
...
@@ -49,37 +55,62 @@ public class HttpsUtil {
static
{
RequestConfig
.
Builder
configBuilder
=
RequestConfig
.
custom
();
// 设置连接超时
configBuilder
.
setConnectTimeout
(
10
*
1000
);
configBuilder
.
setConnectTimeout
(
10
*
1000
);
// 设置读取超时
configBuilder
.
setSocketTimeout
(
10
*
10000
);
configBuilder
.
setSocketTimeout
(
10
*
10000
);
requestConfig
=
configBuilder
.
build
();
}
public
static
CloseableHttpClient
trustAll
()
{
// 配置,发送https请求时,忽略ssl证书认证(否则会报错没有证书)
SSLContext
sslContext
=
null
;
try
{
sslContext
=
SSLContexts
.
custom
().
loadTrustMaterial
(
null
,
new
TrustStrategy
()
{
@Override
public
boolean
isTrusted
(
X509Certificate
[]
x509Certificates
,
String
s
)
throws
CertificateException
{
return
true
;
}
}).
build
();
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyManagementException
e
)
{
e
.
printStackTrace
();
}
catch
(
KeyStoreException
e
)
{
e
.
printStackTrace
();
}
//创建httpClient
CloseableHttpClient
client
=
HttpClients
.
custom
().
setSSLContext
(
sslContext
)
.
setSSLHostnameVerifier
(
new
NoopHostnameVerifier
()).
build
();
return
client
;
}
/**
* 发送无参数的GET请求
*/
public
static
String
doGet
(
String
url
)
{
return
doGet
(
url
,
null
);
return
doGet
(
url
,
null
);
}
/**
* 发送有参数的GET请求,参数为MAP key-value格式
*/
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
params
)
{
List
<
String
>
paramList
=
new
ArrayList
<
String
>();
if
(
params
!=
null
)
{
List
<
String
>
paramList
=
new
ArrayList
<
String
>();
if
(
params
!=
null
)
{
for
(
String
key
:
params
.
keySet
())
{
paramList
.
add
(
key
+
"="
+
params
.
get
(
key
));
paramList
.
add
(
key
+
"="
+
params
.
get
(
key
));
}
if
(
paramList
.
size
()>
0
)
{
if
(
url
.
indexOf
(
"?"
)==-
1
)
{
url
=
url
+
"?"
+
StringUtil
.
join
(
"&"
,
paramList
);
}
else
{
url
=
url
+
"&"
+
StringUtil
.
join
(
"&"
,
paramList
);
if
(
paramList
.
size
()
>
0
)
{
if
(
url
.
indexOf
(
"?"
)
==
-
1
)
{
url
=
url
+
"?"
+
StringUtil
.
join
(
"&"
,
paramList
);
}
else
{
url
=
url
+
"&"
+
StringUtil
.
join
(
"&"
,
paramList
);
}
}
}
System
.
out
.
println
(
"doGet--"
+
url
);
System
.
out
.
println
(
"doGet--"
+
url
);
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
HttpGet
httpGet
=
new
HttpGet
(
url
.
replaceAll
(
" "
,
"%20"
));
httpGet
.
setConfig
(
requestConfig
);
...
...
@@ -95,14 +126,14 @@ public class HttpsUtil {
throw
new
HttpRequestException
(
e
);
}
finally
{
try
{
if
(
entity
!=
null
)
{
if
(
entity
!=
null
)
{
EntityUtils
.
consume
(
entity
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
if
(
response
!=
null
)
{
if
(
response
!=
null
)
{
response
.
close
();
}
}
catch
(
IOException
e
)
{
...
...
@@ -116,14 +147,18 @@ public class HttpsUtil {
}
}
}
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
;
try
{
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
);
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
GET_URL
.
openConnection
()
:
GET_URL
.
openConnection
(
proxy
));
if
(
sslSocketFactory
!=
null
)
{
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
GET_URL
.
openConnection
()
:
GET_URL
.
openConnection
(
proxy
));
if
(
sslSocketFactory
!=
null
)
{
connection
.
setSSLSocketFactory
(
sslSocketFactory
);
}
connection
.
setRequestMethod
(
"GET"
);
...
...
@@ -143,18 +178,21 @@ public class HttpsUtil {
}
}
public
static
String
post
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
headers
,
SSLSocketFactory
sslSocketFactory
,
Proxy
proxy
)
throws
HttpRequestException
{
System
.
out
.
println
(
"----"
+
HttpUtil
.
toQueryString
(
parameters
));
public
static
String
post
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
headers
,
SSLSocketFactory
sslSocketFactory
,
Proxy
proxy
)
throws
HttpRequestException
{
System
.
out
.
println
(
"----"
+
HttpUtil
.
toQueryString
(
parameters
));
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
;
try
{
byte
[]
bytes
=
text
==
null
?
new
byte
[
0
]
:
text
.
getBytes
();
URL
POST_URL
=
new
URL
(
requestUrl
);
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
POST_URL
.
openConnection
()
:
POST_URL
.
openConnection
(
proxy
));
if
(
sslSocketFactory
!=
null
)
{
connection
=
(
HttpsURLConnection
)
(
proxy
==
null
?
POST_URL
.
openConnection
()
:
POST_URL
.
openConnection
(
proxy
));
if
(
sslSocketFactory
!=
null
)
{
connection
.
setSSLSocketFactory
(
sslSocketFactory
);
}
...
...
@@ -196,7 +234,7 @@ public class HttpsUtil {
* @return
*/
public
static
String
doPostForXml
(
String
url
,
String
xml
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
CloseableHttpClient
httpClient
=
trustAll
();
//
HttpClients.createDefault();
HttpPost
httpPost
=
new
HttpPost
(
url
);
httpPost
.
setConfig
(
requestConfig
);
CloseableHttpResponse
response
=
null
;
...
...
@@ -247,24 +285,20 @@ public class HttpsUtil {
*/
public
static
String
doPostForJson
(
String
url
,
String
json
)
{
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
;
}
public
static
String
doPostForJson
(
String
url
,
String
json
,
Map
<
String
,
String
>
headers
)
{
String
result
=
HttpUtil
.
post
(
url
,
json
,
headers
,
null
);
public
static
String
doPostForJson
(
String
url
,
String
json
,
Map
<
String
,
String
>
headers
)
{
String
result
=
HttpUtil
.
post
(
url
,
json
,
headers
,
null
);
return
result
;
}
/**
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
*
* @param url
* 请求地址 form表单url地址
* @param filePath
* 文件在服务器保存路径
* @param url 请求地址 form表单url地址
* @param filePath 文件在服务器保存路径
* @return String url的响应信息返回值
* @throws IOException
*/
...
...
@@ -364,10 +398,8 @@ public class HttpsUtil {
/**
* 调用证书进行退款 WX提供
*
* @param servicePartner
* 商户号
* @param sslPath
* 证书地址 "D:/10016225.p12"
* @param servicePartner 商户号
* @param sslPath 证书地址 "D:/10016225.p12"
* @throws Exception
*/
public
static
String
doPostBySSL
(
String
servicePartner
,
String
sslPath
,
String
xml
,
String
requestUrl
)
...
...
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