Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
smartDevices-platform-service
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 87e13686
authored
Mar 05, 2021
by
111-PC\111
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]http的put和delete方法添加
1 parent
f9907ac6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
126 additions
and
47 deletions
src/main/java/cn/com/glsx/modules/service/DeviceService.java
src/main/java/cn/com/glsx/modules/utils/HttpUtil.java
src/main/java/cn/com/glsx/modules/service/DeviceService.java
View file @
87e1368
...
...
@@ -5,7 +5,6 @@ import cn.com.glsx.modules.model.param.Function;
import
cn.com.glsx.modules.model.param.ResponseBody
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
...
...
src/main/java/cn/com/glsx/modules/utils/HttpUtil.java
View file @
87e1368
...
...
@@ -22,8 +22,10 @@ import org.apache.http.client.HttpClient;
import
org.apache.http.client.HttpRequestRetryHandler
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.HttpDelete
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.methods.HttpPut
;
import
org.apache.http.client.protocol.HttpClientContext
;
import
org.apache.http.client.utils.HttpClientUtils
;
import
org.apache.http.config.Registry
;
...
...
@@ -175,7 +177,7 @@ public class HttpUtil {
}
/**
*
兼容旧版本工具类
*
http发送post请求
*
* @param url
* @return
...
...
@@ -234,6 +236,95 @@ public class HttpUtil {
}
}
/**
* 作者:sanri <br/>
* 时间:2017-10-19下午2:25:40<br/>
* 功能:向指定 url 提交数据 <br/>
*
* @param url
* @param data
* @return
* @throws IOException
*/
public
static
String
putData
(
String
url
,
String
data
,
Map
<
String
,
String
>
headerParams
)
throws
IOException
,
HTTPException
{
HttpClient
httpClient
=
getHttpClient
();
//请求头,请求体数据封装
HttpPut
postMethod
=
new
HttpPut
(
url
);
postMethod
.
addHeader
(
"Content-Type"
,
"application/json"
);
if
(!
headerParams
.
isEmpty
())
{
headerParams
.
forEach
((
key
,
value
)
->
{
postMethod
.
addHeader
(
key
,
value
);
});
}
postMethod
.
addHeader
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0"
);
StringEntity
dataEntity
=
new
StringEntity
(
data
,
JSON_UTF8
);
postMethod
.
setEntity
(
dataEntity
);
HttpResponse
response
=
null
;
try
{
//开始请求,记录请求数据和请求时间
response
=
httpClient
.
execute
(
postMethod
);
//获取响应
HttpEntity
entity
=
response
.
getEntity
();
StatusLine
statusLine
=
response
.
getStatusLine
();
int
statusCode
=
statusLine
.
getStatusCode
();
if
(
statusCode
!=
200
)
{
HTTPException
exception
=
new
HTTPException
(
statusCode
);
logger
.
error
(
"请求地址"
+
url
+
"出错,http 状态码为:"
+
statusCode
,
exception
);
throw
exception
;
}
String
retData
=
EntityUtils
.
toString
(
entity
,
JSON_UTF8
.
getCharset
());
return
retData
;
}
catch
(
ClientProtocolException
e
)
{
throw
new
IllegalArgumentException
(
"客户端协议错误 ,检查 url 配置 url: "
+
url
,
e
);
}
finally
{
HttpClientUtils
.
closeQuietly
(
response
);
}
}
/**
* 作者:sanri <br/>
* 时间:2017-10-19下午2:25:40<br/>
* 功能:向指定 url 提交数据 <br/>
*
* @param url
* @return
* @throws IOException
*/
public
static
String
deleteData
(
String
url
,
Map
<
String
,
String
>
headerParams
)
throws
IOException
,
HTTPException
{
HttpClient
httpClient
=
getHttpClient
();
//请求头,请求体数据封装
HttpDelete
httpDelete
=
new
HttpDelete
(
url
);
httpDelete
.
addHeader
(
"Content-Type"
,
"application/json"
);
if
(!
headerParams
.
isEmpty
())
{
headerParams
.
forEach
((
key
,
value
)
->
{
httpDelete
.
addHeader
(
key
,
value
);
});
}
httpDelete
.
addHeader
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0"
);
HttpResponse
response
=
null
;
try
{
//开始请求,记录请求数据和请求时间
response
=
httpClient
.
execute
(
httpDelete
);
//获取响应
HttpEntity
entity
=
response
.
getEntity
();
StatusLine
statusLine
=
response
.
getStatusLine
();
int
statusCode
=
statusLine
.
getStatusCode
();
if
(
statusCode
!=
200
)
{
HTTPException
exception
=
new
HTTPException
(
statusCode
);
logger
.
error
(
"请求地址"
+
url
+
"出错,http 状态码为:"
+
statusCode
,
exception
);
throw
exception
;
}
String
retData
=
EntityUtils
.
toString
(
entity
,
JSON_UTF8
.
getCharset
());
return
retData
;
}
catch
(
ClientProtocolException
e
)
{
throw
new
IllegalArgumentException
(
"客户端协议错误 ,检查 url 配置 url: "
+
url
,
e
);
}
finally
{
HttpClientUtils
.
closeQuietly
(
response
);
}
}
/**
* 作者:sanri <br/>
* 时间:2018-3-28下午8:57:50<br/>
...
...
@@ -296,65 +387,54 @@ public class HttpUtil {
return
data
;
}
public
static
JSONObject
getJSON
(
String
url
,
Map
<
String
,
String
>
params
,
Charset
charset
,
Map
<
String
,
String
>
headerParams
)
throws
IOException
{
String
retData
=
getData
(
url
,
params
,
charset
,
headerParams
);
/**
* http发送get请求
*
* @param url
* @param params
* @param headerParams
* @return
* @throws IOException
*/
public
static
JSONObject
sendGet
(
String
url
,
Map
<
String
,
String
>
params
,
Map
<
String
,
String
>
headerParams
)
throws
IOException
{
String
retData
=
getData
(
url
,
params
,
Consts
.
UTF_8
,
headerParams
);
if
(
StringUtils
.
isNotBlank
(
retData
))
{
return
JSONObject
.
parseObject
(
retData
);
}
return
new
JSONObject
();
}
public
static
String
postFormData
(
String
url
,
Map
<
String
,
String
>
params
,
Charset
charset
)
throws
IOException
{
CloseableHttpClient
httpClient
=
getHttpClient
();
List
<
NameValuePair
>
nameValuePairs
=
transferParam
(
params
);
HttpPost
postMethod
=
new
HttpPost
(
url
);
if
(
CollectionUtils
.
isNotEmpty
(
nameValuePairs
))
{
HttpEntity
entity
=
new
UrlEncodedFormEntity
(
nameValuePairs
,
charset
);
postMethod
.
setEntity
(
entity
);
}
//TODO 测试用
postMethod
.
addHeader
(
"Cookie"
,
"JSESSIONID=node0oze509ig0of0vksk2dshvoi12.node0; fileDownload=true"
);
postMethod
.
addHeader
(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
);
HttpResponse
response
=
null
;
try
{
response
=
httpClient
.
execute
(
postMethod
);
StatusLine
statusLine
=
response
.
getStatusLine
();
int
statusCode
=
statusLine
.
getStatusCode
();
if
(
statusCode
!=
200
)
{
//解决地址重定向问题
if
(
statusCode
==
302
)
{
Header
firstHeader
=
response
.
getFirstHeader
(
"location"
);
String
location
=
firstHeader
.
getValue
();
return
postFormData
(
location
,
params
,
charset
);
}
HTTPException
exception
=
new
HTTPException
(
statusCode
);
logger
.
error
(
"请求地址"
+
url
+
"出错,http 状态码为:"
+
statusCode
,
exception
);
throw
exception
;
}
HttpEntity
msgEntity
=
response
.
getEntity
();
String
message
=
EntityUtils
.
toString
(
msgEntity
,
charset
);
return
message
;
}
finally
{
HttpClientUtils
.
closeQuietly
(
response
);
/**
* http的put方法
*
* @param url 请求url
* @param json 请求body
* @param headerParams 请求参数
* @return
* @throws IOException
*/
public
static
JSONObject
sendPut
(
String
url
,
String
json
,
Map
<
String
,
String
>
headerParams
)
throws
IOException
{
String
formData
=
putData
(
url
,
json
,
headerParams
);
if
(
StringUtils
.
isNotBlank
(
formData
))
{
return
JSONObject
.
parseObject
(
formData
);
}
return
new
JSONObject
();
}
/**
* 作者:sanri <br/>
* 时间:2017-11-7下午6:24:30<br/>
* 功能:调用地址获取 json 数据 <br/>
* http发送delete请求
*
* @param url
* @param
params
* @param url
url
* @param
headerParams 请求参数
* @return
* @throws IllegalArgumentException
* @throws IOException
*/
public
static
JSONObject
sendGet
(
String
url
,
Map
<
String
,
String
>
params
,
Map
<
String
,
String
>
headerParams
)
throws
IOException
{
return
getJSON
(
url
,
params
,
Consts
.
UTF_8
,
headerParams
);
public
static
JSONObject
sendDelete
(
String
url
,
Map
<
String
,
String
>
headerParams
)
throws
IOException
{
String
retData
=
deleteData
(
url
,
headerParams
);
if
(
StringUtils
.
isNotBlank
(
retData
))
{
return
JSONObject
.
parseObject
(
retData
);
}
return
new
JSONObject
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
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 post a comment