记得上下班打卡 | 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
88567036
Commit
88567036
authored
Jul 08, 2021
by
jiangxiulong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test http
parent
b83bef9f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
190 additions
and
4 deletions
+190
-4
HttpsUtils.java
...quidnet/client/admin/zhengzai/kylin/utils/HttpsUtils.java
+140
-0
ShunfengSignUtils.java
.../client/admin/zhengzai/kylin/utils/ShunfengSignUtils.java
+50
-4
No files found.
liquidnet-bus-client/liquidnet-client-admin/liquidnet-client-admin-zhengzai/src/main/java/com/liquidnet/client/admin/zhengzai/kylin/utils/HttpsUtils.java
0 → 100644
View file @
88567036
package
com
.
liquidnet
.
client
.
admin
.
zhengzai
.
kylin
.
utils
;
import
org.apache.commons.collections.MapUtils
;
import
org.apache.http.*
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.config.Registry
;
import
org.apache.http.config.RegistryBuilder
;
import
org.apache.http.conn.socket.ConnectionSocketFactory
;
import
org.apache.http.conn.socket.PlainConnectionSocketFactory
;
import
org.apache.http.conn.ssl.NoopHostnameVerifier
;
import
org.apache.http.conn.ssl.SSLConnectionSocketFactory
;
import
org.apache.http.conn.ssl.TrustStrategy
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.ssl.SSLContextBuilder
;
import
org.apache.http.util.EntityUtils
;
import
java.io.IOException
;
import
java.security.cert.CertificateException
;
import
java.security.cert.X509Certificate
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
public
class
HttpsUtils
{
private
static
final
String
HTTP
=
"http"
;
private
static
final
String
HTTPS
=
"https"
;
private
static
SSLConnectionSocketFactory
sslsf
=
null
;
private
static
PoolingHttpClientConnectionManager
cm
=
null
;
private
static
SSLContextBuilder
builder
=
null
;
static
{
try
{
builder
=
new
SSLContextBuilder
();
// 全部信任 不做身份鉴定
builder
.
loadTrustMaterial
(
null
,
new
TrustStrategy
()
{
@Override
public
boolean
isTrusted
(
X509Certificate
[]
x509Certificates
,
String
s
)
throws
CertificateException
{
return
true
;
}
});
sslsf
=
new
SSLConnectionSocketFactory
(
builder
.
build
(),
new
String
[]{
"SSLv2Hello"
,
"SSLv3"
,
"TLSv1"
,
"TLSv1.2"
},
null
,
NoopHostnameVerifier
.
INSTANCE
);
Registry
<
ConnectionSocketFactory
>
registry
=
RegistryBuilder
.<
ConnectionSocketFactory
>
create
()
.
register
(
HTTP
,
new
PlainConnectionSocketFactory
())
.
register
(
HTTPS
,
sslsf
)
.
build
();
cm
=
new
PoolingHttpClientConnectionManager
(
registry
);
cm
.
setMaxTotal
(
200
);
//max connection
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
/**
* httpClient post请求
*
* @param url 请求url
* @param header 头部信息
* @param param 请求参数 form提交适用
* @param entity 请求实体 json/xml提交适用
* @return 可能为空 需要处理
* @throws Exception
*/
public
static
String
post
(
String
url
,
Map
<
String
,
String
>
header
,
Map
<
String
,
String
>
param
,
HttpEntity
entity
)
throws
Exception
{
String
result
=
""
;
CloseableHttpClient
httpClient
=
null
;
try
{
httpClient
=
getHttpClient
();
HttpPost
httpPost
=
new
HttpPost
(
url
);
// 设置头信息
if
(
MapUtils
.
isNotEmpty
(
header
))
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
header
.
entrySet
())
{
httpPost
.
addHeader
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
// 设置请求参数
if
(
MapUtils
.
isNotEmpty
(
param
))
{
List
<
NameValuePair
>
formparams
=
new
ArrayList
<
NameValuePair
>();
for
(
Map
.
Entry
<
String
,
String
>
entry
:
param
.
entrySet
())
{
//给参数赋值
formparams
.
add
(
new
BasicNameValuePair
(
entry
.
getKey
(),
entry
.
getValue
()));
}
UrlEncodedFormEntity
urlEncodedFormEntity
=
new
UrlEncodedFormEntity
(
formparams
,
Consts
.
UTF_8
);
httpPost
.
setEntity
(
urlEncodedFormEntity
);
}
// 设置实体 优先级高
if
(
entity
!=
null
)
{
httpPost
.
setEntity
(
entity
);
}
HttpResponse
httpResponse
=
httpClient
.
execute
(
httpPost
);
int
statusCode
=
httpResponse
.
getStatusLine
().
getStatusCode
();
if
(
statusCode
==
HttpStatus
.
SC_OK
)
{
HttpEntity
resEntity
=
httpResponse
.
getEntity
();
result
=
EntityUtils
.
toString
(
resEntity
);
}
else
{
readHttpResponse
(
httpResponse
);
}
}
catch
(
Exception
e
)
{
throw
e
;
}
finally
{
if
(
httpClient
!=
null
)
{
httpClient
.
close
();
}
}
return
result
;
}
public
static
CloseableHttpClient
getHttpClient
()
throws
Exception
{
CloseableHttpClient
httpClient
=
HttpClients
.
custom
()
.
setSSLSocketFactory
(
sslsf
)
.
setConnectionManager
(
cm
)
.
setConnectionManagerShared
(
true
)
.
build
();
return
httpClient
;
}
public
static
String
readHttpResponse
(
HttpResponse
httpResponse
)
throws
ParseException
,
IOException
{
StringBuilder
builder
=
new
StringBuilder
();
// 获取响应消息实体
HttpEntity
entity
=
httpResponse
.
getEntity
();
// 响应状态
builder
.
append
(
"status:"
+
httpResponse
.
getStatusLine
());
builder
.
append
(
"headers:"
);
HeaderIterator
iterator
=
httpResponse
.
headerIterator
();
while
(
iterator
.
hasNext
())
{
builder
.
append
(
"\t"
+
iterator
.
next
());
}
// 判断响应实体是否为空
if
(
entity
!=
null
)
{
String
responseString
=
EntityUtils
.
toString
(
entity
);
builder
.
append
(
"response length:"
+
responseString
.
length
());
builder
.
append
(
"response content:"
+
responseString
.
replace
(
"\r\n"
,
""
));
}
return
builder
.
toString
();
}
}
\ No newline at end of file
liquidnet-bus-client/liquidnet-client-admin/liquidnet-client-admin-zhengzai/src/main/java/com/liquidnet/client/admin/zhengzai/kylin/utils/ShunfengSignUtils.java
View file @
88567036
package
com
.
liquidnet
.
client
.
admin
.
zhengzai
.
kylin
.
utils
;
import
com.liquidnet.client.admin.common.utils.http.HttpUtils
;
import
com.liquidnet.commons.lang.util.JsonUtils
;
import
com.liquidnet.service.base.UserPathDto
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -10,13 +11,17 @@ import org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.conn.ssl.NoopHostnameVerifier
;
import
org.apache.http.conn.ssl.SSLConnectionSocketFactory
;
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.util.EntityUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
javax.net.ssl.SSLContext
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -73,13 +78,50 @@ public class ShunfengSignUtils {
headers
.
put
(
"Accept"
,
"application/json"
);
Map
<
String
,
String
>
querys
=
new
HashMap
<>();
try
{
HttpResponse
httpResponse
=
HttpClientUtils
.
doPost
(
URL
,
url
,
""
,
headers
,
querys
,
body
);
if
(
httpResponse
.
getStatusLine
().
getStatusCode
()
==
200
)
{
// HttpResponse httpResponse = HttpClientUtils.doPost(URL, url, "", headers, querys, body);
// HttpEntity httpEntity = new HttpEntity(body, "utf-8");
// HttpEntity httpEntity = new HttpEntity(body, "utf-8");
HttpEntity
httpEntity
=
new
StringEntity
(
body
,
"utf-8"
);
String
post
=
HttpsUtils
.
post
(
URL
+
url
,
headers
,
querys
,
httpEntity
);
return
post
;
// HttpResponse httpResponse = HttpsUtils.post(URL+url, headers, querys, httpEntity);
/*if (httpResponse.getStatusLine().getStatusCode() == 200) {
System.out.println("发送请求成功");
HttpEntity entity = httpResponse.getEntity();
// 发送响应且编码UTF8!!!
return EntityUtils.toString(entity, "utf-8");
}
}*/
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"发送请求失败"
);
e
.
printStackTrace
();
return
e
.
getMessage
();
}
// return null;
}
/**
* 生成签名并请求
* @param hbody 请求body
* @return
*/
public
String
generateSignatureAndRequestNew2
(
Map
<
String
,
String
>
hbody
,
String
url
)
{
long
currentTimeMillis
=
System
.
currentTimeMillis
();
// 时间戳
String
timestamp
=
currentTimeMillis
+
""
;
hbody
.
put
(
"companyId"
,
APP_ID
);
String
body
=
JsonUtils
.
toJson
(
hbody
);
// 生成签名
String
sign
=
genSign
(
timestamp
,
body
);
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"sendAppId"
,
APP_ID
);
headers
.
put
(
"timestamp"
,
timestamp
);
headers
.
put
(
"sign"
,
sign
);
headers
.
put
(
"Content-Type"
,
"application/json;charset=utf-8"
);
headers
.
put
(
"Accept"
,
"application/json"
);
try
{
String
sss
=
HttpUtils
.
sendSSLPost
(
URL
+
url
,
body
);
System
.
out
.
println
(
"发送请求成功"
);
System
.
out
.
println
(
sss
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"发送请求失败"
);
e
.
printStackTrace
();
...
...
@@ -101,7 +143,11 @@ public class ShunfengSignUtils {
// 生成签名
String
sign
=
genSign
(
timestamp
,
body
);
CloseableHttpClient
client
=
HttpClients
.
createDefault
();
CloseableHttpClient
client
=
HttpClients
.
createSystem
();
/*SSLContext sslContext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
NoopHostnameVerifier.INSTANCE);*/
// 请求下单地址
HttpPost
httpPost
=
new
HttpPost
(
URL
+
url
);
// sendAppId(sendAppId需赋值appId)
...
...
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