记得上下班打卡 | git大法好,push需谨慎
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
liquidnet-bus-v1
Project
Project
Details
Activity
Releases
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
董敬伟
liquidnet-bus-v1
Commits
18fe0cc9
Commit
18fe0cc9
authored
Jul 21, 2021
by
胡佳晨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改 httpClient request
parent
77b920c8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
257 additions
and
159 deletions
+257
-159
RestTemplateConfig.java
...com/liquidnet/commons/lang/config/RestTemplateConfig.java
+93
-0
HttpUtil.java
...c/main/java/com/liquidnet/commons/lang/util/HttpUtil.java
+164
-159
No files found.
liquidnet-bus-common/liquidnet-common-base/src/main/java/com/liquidnet/commons/lang/config/RestTemplateConfig.java
0 → 100644
View file @
18fe0cc9
package
com
.
liquidnet
.
commons
.
lang
.
config
;
import
org.apache.http.Header
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy
;
import
org.apache.http.impl.client.DefaultHttpRequestRetryHandler
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
;
import
org.apache.http.message.BasicHeader
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.client.ClientHttpRequestFactory
;
import
org.springframework.http.client.HttpComponentsClientHttpRequestFactory
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.StringHttpMessageConverter
;
import
org.springframework.web.client.RestTemplate
;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
@Configuration
public
class
RestTemplateConfig
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RestTemplateConfig
.
class
);
@Bean
public
RestTemplate
restTemplate
()
{
// 添加内容转换器,使用默认的内容转换器
RestTemplate
restTemplate
=
new
RestTemplate
(
httpRequestFactory
());
// 设置编码格式为UTF-8
List
<
HttpMessageConverter
<?>>
converterList
=
restTemplate
.
getMessageConverters
();
HttpMessageConverter
<?>
converterTarget
=
null
;
for
(
HttpMessageConverter
<?>
item
:
converterList
)
{
if
(
item
.
getClass
()
==
StringHttpMessageConverter
.
class
)
{
converterTarget
=
item
;
break
;
}
}
if
(
converterTarget
!=
null
)
{
converterList
.
remove
(
converterTarget
);
}
HttpMessageConverter
<?>
converter
=
new
StringHttpMessageConverter
(
StandardCharsets
.
UTF_8
);
converterList
.
add
(
1
,
converter
);
return
restTemplate
;
}
@Bean
public
ClientHttpRequestFactory
httpRequestFactory
()
{
return
new
HttpComponentsClientHttpRequestFactory
(
httpClient
());
}
@Bean
public
HttpClient
httpClient
()
{
// 长连接保持30秒
PoolingHttpClientConnectionManager
connectionManager
=
new
PoolingHttpClientConnectionManager
(
30
,
TimeUnit
.
SECONDS
);
//设置整个连接池最大连接数 根据自己的场景决定
connectionManager
.
setMaxTotal
(
500
);
//同路由的并发数,路由是对maxTotal的细分
connectionManager
.
setDefaultMaxPerRoute
(
500
);
//requestConfig
RequestConfig
requestConfig
=
RequestConfig
.
custom
()
//服务器返回数据(response)的时间,超过该时间抛出read timeout
.
setSocketTimeout
(
10000
)
//连接上服务器(握手成功)的时间,超出该时间抛出connect timeout
.
setConnectTimeout
(
5000
)
//从连接池中获取连接的超时时间,超过该时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
.
setConnectionRequestTimeout
(
500
)
.
build
();
//headers
List
<
Header
>
headers
=
new
ArrayList
<>();
headers
.
add
(
new
BasicHeader
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"
));
headers
.
add
(
new
BasicHeader
(
"Accept-Encoding"
,
"gzip,deflate"
));
headers
.
add
(
new
BasicHeader
(
"Accept-Language"
,
"zh-CN"
));
headers
.
add
(
new
BasicHeader
(
"Connection"
,
"Keep-Alive"
));
headers
.
add
(
new
BasicHeader
(
"Content-type"
,
"application/json;charset=UTF-8"
));
return
HttpClientBuilder
.
create
()
.
setDefaultRequestConfig
(
requestConfig
)
.
setConnectionManager
(
connectionManager
)
.
setDefaultHeaders
(
headers
)
// 保持长连接配置,需要在头添加Keep-Alive
.
setKeepAliveStrategy
(
new
DefaultConnectionKeepAliveStrategy
())
//重试次数,默认是3次,没有开启
.
setRetryHandler
(
new
DefaultHttpRequestRetryHandler
(
2
,
true
))
.
build
();
}
}
liquidnet-bus-common/liquidnet-common-base/src/main/java/com/liquidnet/commons/lang/util/HttpUtil.java
View file @
18fe0cc9
package
com
.
liquidnet
.
commons
.
lang
.
util
;
package
com
.
liquidnet
.
commons
.
lang
.
util
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.*
;
import
org.springframework.http.*
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.MultiValueMap
;
...
@@ -11,163 +12,167 @@ import java.util.List;
...
@@ -11,163 +12,167 @@ import java.util.List;
import
java.util.Map
;
import
java.util.Map
;
public
class
HttpUtil
{
public
class
HttpUtil
{
/**
* get请求
@Autowired
*
private
static
RestTemplate
restTemplate
;
* @param url
* @param params 请求参数
/**
* @return
* get请求
*/
*
public
static
String
get
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* @param url
return
get
(
url
,
params
,
null
);
* @param params 请求参数
}
* @return
*/
/**
public
static
String
get
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* get请求
return
get
(
url
,
params
,
null
);
*
}
* @param url
* @param params 请求参数
/**
* @param headers 请求头
* get请求
* @return
*
*/
* @param url
public
static
String
get
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* @param params 请求参数
return
request
(
url
,
params
,
headers
,
HttpMethod
.
GET
);
* @param headers 请求头
}
* @return
*/
/**
public
static
String
get
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* post请求
return
request
(
url
,
params
,
headers
,
HttpMethod
.
GET
);
*
}
* @param url
* @param params 请求参数
/**
* @return
* post请求
*/
*
public
static
String
post
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* @param url
return
post
(
url
,
params
,
null
);
* @param params 请求参数
}
* @return
*/
/**
public
static
String
post
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* post请求
return
post
(
url
,
params
,
null
);
*
}
* @param url
* @param params 请求参数
/**
* @param headers 请求头
* post请求
* @return
*
*/
* @param url
public
static
String
post
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* @param params 请求参数
return
request
(
url
,
params
,
headers
,
HttpMethod
.
POST
);
* @param headers 请求头
}
* @return
*/
/**
public
static
String
post
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* put请求
return
request
(
url
,
params
,
headers
,
HttpMethod
.
POST
);
*
}
* @param url
* @param params 请求参数
/**
* @return
* put请求
*/
*
public
static
String
put
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* @param url
return
put
(
url
,
params
,
null
);
* @param params 请求参数
}
* @return
*/
/**
public
static
String
put
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* put请求
return
put
(
url
,
params
,
null
);
*
}
* @param url
* @param params 请求参数
/**
* @param headers 请求头
* put请求
* @return
*
*/
* @param url
public
static
String
put
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* @param params 请求参数
return
request
(
url
,
params
,
headers
,
HttpMethod
.
PUT
);
* @param headers 请求头
}
* @return
*/
/**
public
static
String
put
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* delete请求
return
request
(
url
,
params
,
headers
,
HttpMethod
.
PUT
);
*
}
* @param url
* @param params 请求参数
/**
* @return
* delete请求
*/
*
public
static
String
delete
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* @param url
return
delete
(
url
,
params
,
null
);
* @param params 请求参数
}
* @return
*/
/**
public
static
String
delete
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
* delete请求
return
delete
(
url
,
params
,
null
);
*
}
* @param url
* @param params 请求参数
/**
* @param headers 请求头
* delete请求
* @return
*
*/
* @param url
public
static
String
delete
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* @param params 请求参数
return
request
(
url
,
params
,
headers
,
HttpMethod
.
DELETE
);
* @param headers 请求头
}
* @return
*/
/**
public
static
String
delete
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
)
{
* 表单请求
return
request
(
url
,
params
,
headers
,
HttpMethod
.
DELETE
);
*
}
* @param url
* @param params 请求参数
/**
* @param headers 请求头
* 表单请求
* @param method 请求方式
*
* @return
* @param url
*/
* @param params 请求参数
public
static
String
request
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
,
HttpMethod
method
)
{
* @param headers 请求头
if
(
params
==
null
)
{
* @param method 请求方式
params
=
new
LinkedMultiValueMap
<>();
* @return
}
*/
return
request
(
url
,
params
,
headers
,
method
,
MediaType
.
APPLICATION_FORM_URLENCODED
);
public
static
String
request
(
String
url
,
MultiValueMap
<
String
,
String
>
params
,
MultiValueMap
<
String
,
String
>
headers
,
HttpMethod
method
)
{
}
if
(
params
==
null
)
{
params
=
new
LinkedMultiValueMap
<>();
/**
}
* http请求
return
request
(
url
,
params
,
headers
,
method
,
MediaType
.
APPLICATION_FORM_URLENCODED
);
*
}
* @param url
* @param params 请求参数
/**
* @param headers 请求头
* http请求
* @param method 请求方式
*
* @param mediaType 参数类型
* @param url
* @return
* @param params 请求参数
*/
* @param headers 请求头
public
static
String
request
(
String
url
,
Object
params
,
MultiValueMap
<
String
,
String
>
headers
,
HttpMethod
method
,
MediaType
mediaType
)
{
* @param method 请求方式
if
(
url
==
null
||
url
.
trim
().
isEmpty
())
{
* @param mediaType 参数类型
return
null
;
* @return
}
*/
RestTemplate
client
=
new
RestTemplate
();
public
static
String
request
(
String
url
,
Object
params
,
MultiValueMap
<
String
,
String
>
headers
,
HttpMethod
method
,
MediaType
mediaType
)
{
// header
if
(
url
==
null
||
url
.
trim
().
isEmpty
())
{
HttpHeaders
httpHeaders
=
new
HttpHeaders
();
return
null
;
if
(
headers
!=
null
)
{
}
httpHeaders
.
addAll
(
headers
);
HttpEntity
<
Object
>
httpEntity
;
}
if
(
headers
!=
null
)
{
// 提交方式:表单、json
// header
httpHeaders
.
setContentType
(
mediaType
);
HttpHeaders
httpHeaders
=
new
HttpHeaders
();
HttpEntity
<
Object
>
httpEntity
=
new
HttpEntity
(
params
,
httpHeaders
);
httpHeaders
.
addAll
(
headers
);
ResponseEntity
<
String
>
response
=
client
.
exchange
(
url
,
method
,
httpEntity
,
String
.
class
);
httpHeaders
.
setContentType
(
mediaType
);
return
response
.
getBody
();
httpEntity
=
new
HttpEntity
(
params
,
httpHeaders
);
}
}
else
{
httpEntity
=
new
HttpEntity
(
params
,
null
);
}
// 提交方式:表单、json
ResponseEntity
<
String
>
response
=
restTemplate
.
exchange
(
url
,
method
,
httpEntity
,
String
.
class
);
private
static
final
String
PHP_API_KEY
=
"R7tXY9smPQPG9Ku5yI0u6sfnlckmk04V"
;
return
response
.
getBody
();
}
public
static
String
postToPhpApi
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
params
.
add
(
"sign"
,
processForPhpApi
(
params
).
concat
(
"&key="
).
concat
(
PHP_API_KEY
).
toUpperCase
());
return
post
(
url
,
params
,
null
);
private
static
final
String
PHP_API_KEY
=
"R7tXY9smPQPG9Ku5yI0u6sfnlckmk04V"
;
}
public
static
String
postToPhpApi
(
String
url
,
MultiValueMap
<
String
,
String
>
params
)
{
private
static
String
processForPhpApi
(
MultiValueMap
<
String
,
String
>
map
)
{
params
.
add
(
"sign"
,
processForPhpApi
(
params
).
concat
(
"&key="
).
concat
(
PHP_API_KEY
).
toUpperCase
());
StringBuilder
sb
=
new
StringBuilder
();
return
post
(
url
,
params
,
null
);
for
(
Map
.
Entry
<
String
,
List
<
String
>>
entry
:
map
.
entrySet
())
{
}
sb
.
append
(
entry
.
getKey
()).
append
(
"="
).
append
(
entry
.
getValue
().
get
(
0
)).
append
(
"&"
);
}
private
static
String
processForPhpApi
(
MultiValueMap
<
String
,
String
>
map
)
{
String
targetStr
=
sb
.
substring
(
0
,
sb
.
length
()
-
1
);
StringBuilder
sb
=
new
StringBuilder
();
try
{
for
(
Map
.
Entry
<
String
,
List
<
String
>>
entry
:
map
.
entrySet
())
{
targetStr
=
URLDecoder
.
decode
(
targetStr
,
"UTF-8"
);
sb
.
append
(
entry
.
getKey
()).
append
(
"="
).
append
(
entry
.
getValue
().
get
(
0
)).
append
(
"&"
);
}
catch
(
UnsupportedEncodingException
e
)
{
}
e
.
printStackTrace
();
String
targetStr
=
sb
.
substring
(
0
,
sb
.
length
()
-
1
);
}
try
{
targetStr
=
targetStr
.
replace
(
"%3D"
,
"="
).
replace
(
"%26"
,
"&"
);
targetStr
=
URLDecoder
.
decode
(
targetStr
,
"UTF-8"
);
return
targetStr
;
}
catch
(
UnsupportedEncodingException
e
)
{
}
e
.
printStackTrace
();
}
targetStr
=
targetStr
.
replace
(
"%3D"
,
"="
).
replace
(
"%26"
,
"&"
);
return
targetStr
;
}
}
}
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