Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
map-parent
Overview
Overview
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
常超
map-parent
Commits
d47a1179
Commit
d47a1179
authored
Mar 19, 2021
by
YANGYANG
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http header通用接口
parent
fb4d8938
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
123 additions
and
0 deletions
+123
-0
common/common_utils/src/main/java/com/founder/commonutils/util/HttpClient.java
+123
-0
No files found.
common/common_utils/src/main/java/com/founder/commonutils/util/HttpClient.java
0 → 100644
View file @
d47a1179
package
com
.
founder
.
commonutils
.
util
;
import
org.apache.http.HttpStatus
;
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.client.utils.URIBuilder
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
org.springframework.stereotype.Component
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.util.*
;
/**
* @ClassName: HttpClient
* @Auther: admin
* @Description: java类作用描述
* @CreateDate: 2021/3/19
* @Version: 1.0
*/
@Component
public
class
HttpClient
{
private
static
RequestConfig
requestConfig
=
null
;
static
{
// 设置请求和传输超时时间
//setConnectTimeout:设置连接超时时间,单位毫秒。
//setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
//setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒
requestConfig
=
RequestConfig
.
custom
().
setSocketTimeout
(
120000
).
setConnectTimeout
(
120000
).
build
();
}
/**
* 请求的参数类型为json
* @param url
* @param json
* @return {username:"",pass:""}
*/
public
static
String
doPostJson
(
String
url
,
Map
<
String
,
String
>
headers
,
String
json
)
throws
Exception
{
// post请求返回结果
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
String
jsonResult
=
null
;
HttpPost
httpPost
=
new
HttpPost
(
url
);
// 设置请求和传输超时时间
httpPost
.
setConfig
(
requestConfig
);
if
(
headers
!=
null
)
{
for
(
String
key
:
headers
.
keySet
())
{
httpPost
.
setHeader
(
key
,
headers
.
get
(
key
));
}
}
try
{
//设置参数解决中文乱码
if
(
null
!=
json
){
StringEntity
entity
=
new
StringEntity
(
json
,
"utf-8"
);
entity
.
setContentEncoding
(
"UTF-8"
);
entity
.
setContentType
(
"application/json"
);
httpPost
.
setEntity
(
entity
);
}
//发送请求
CloseableHttpResponse
result
=
httpClient
.
execute
(
httpPost
);
System
.
out
.
println
(
"StatusCode===="
+
result
.
getStatusLine
().
getStatusCode
());
if
(
result
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
){
// 读取服务器返回的json数据(然后解析)
jsonResult
=
EntityUtils
.
toString
(
result
.
getEntity
(),
"utf-8"
);
}
else
{
throw
new
Exception
();
}
}
catch
(
Exception
e
){
e
.
printStackTrace
();
throw
new
Exception
();
}
finally
{
httpPost
.
releaseConnection
();
}
return
jsonResult
;
}
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
headMap
,
Map
<
String
,
String
>
param
)
{
// 创建客户端连接
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
String
resultString
=
""
;
CloseableHttpResponse
response
=
null
;
try
{
// 创建uri
URIBuilder
builder
=
new
URIBuilder
(
url
);
if
(
param
!=
null
&&
param
.
size
()>
0
)
{
for
(
String
key
:
param
.
keySet
())
{
builder
.
addParameter
(
key
,
param
.
get
(
key
));
}
}
URI
uri
=
builder
.
build
();
// 创建http GET请求
HttpGet
httpGet
=
new
HttpGet
(
uri
);
if
(
headMap
!=
null
&&
!
headMap
.
isEmpty
())
{
for
(
String
key
:
headMap
.
keySet
())
{
httpGet
.
addHeader
(
key
,
headMap
.
get
(
key
));
}
}
// 执行请求
response
=
httpclient
.
execute
(
httpGet
);
// 判断是否请求成功
if
(
response
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
resultString
=
EntityUtils
.
toString
(
response
.
getEntity
(),
"UTF-8"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
// throw new InterServiceException(ResultEnum.REQUEST_URL_ERROR.getCode(),ResultEnum.REQUEST_URL_ERROR.getMessage(),e);
}
finally
{
try
{
if
(
response
!=
null
)
{
response
.
close
();
}
httpclient
.
close
();
}
catch
(
IOException
e
)
{
//throw new InterServiceException(ResultEnum.REQUEST_URL_ERROR.getCode(),ResultEnum.REQUEST_URL_ERROR.getMessage(),e);
}
}
return
resultString
;
}
}
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