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
1eedd960
Commit
1eedd960
authored
Aug 02, 2018
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
2c952ff3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
0 deletions
+81
-0
HttpsUtil.java
src/main/java/com/egolm/common/HttpsUtil.java
+81
-0
No files found.
src/main/java/com/egolm/common/HttpsUtil.java
View file @
1eedd960
package
com
.
egolm
.
common
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.net.Proxy
;
import
java.net.URL
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
javax.net.ssl.HttpsURLConnection
;
import
javax.net.ssl.SSLSocketFactory
;
import
org.apache.http.HttpEntity
;
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.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
com.egolm.common.exception.HttpRequestException
;
...
...
@@ -18,7 +29,77 @@ import com.egolm.common.exception.HttpRequestException;
*
*/
public
class
HttpsUtil
{
private
static
RequestConfig
requestConfig
;
static
{
RequestConfig
.
Builder
configBuilder
=
RequestConfig
.
custom
();
// 设置连接超时
configBuilder
.
setConnectTimeout
(
10
*
1000
);
// 设置读取超时
configBuilder
.
setSocketTimeout
(
10
*
10000
);
requestConfig
=
configBuilder
.
build
();
}
/**
* 发送无参数的GET请求
*/
public
static
String
doGet
(
String
url
)
{
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
){
for
(
String
key
:
params
.
keySet
())
{
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
);
}
}
}
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
)
{
e
.
printStackTrace
();
}
try
{
if
(
response
!=
null
){
response
.
close
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
httpclient
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
public
static
String
get
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
header
,
Proxy
proxy
,
SSLSocketFactory
sslSocketFactory
)
throws
HttpRequestException
{
HttpsURLConnection
connection
=
null
;
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