Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
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
YZG
yzg-util
Commits
0abb62e8
Commit
0abb62e8
authored
Oct 13, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改MQ请求尸体,防止出错
parent
589646f2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
12 deletions
+26
-12
HttpHelper.java
...src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+26
-12
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
View file @
0abb62e8
...
@@ -11,6 +11,7 @@ import java.net.URLConnection;
...
@@ -11,6 +11,7 @@ import java.net.URLConnection;
/**
/**
* HTTP请求工具类
* HTTP请求工具类
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
HttpHelper
{
public
class
HttpHelper
{
...
@@ -65,8 +66,7 @@ public class HttpHelper {
...
@@ -65,8 +66,7 @@ public class HttpHelper {
// 设置通用属性
// 设置通用属性
httpConn
.
setRequestProperty
(
"Accept"
,
"*/*"
);
httpConn
.
setRequestProperty
(
"Accept"
,
"*/*"
);
httpConn
.
setRequestProperty
(
"Connection"
,
"Keep-Alive"
);
httpConn
.
setRequestProperty
(
"Connection"
,
"Keep-Alive"
);
httpConn
.
setRequestProperty
(
"User-Agent"
,
httpConn
.
setRequestProperty
(
"User-Agent"
,
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
);
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
);
// 设置POST方式
// 设置POST方式
httpConn
.
setDoInput
(
true
);
httpConn
.
setDoInput
(
true
);
httpConn
.
setDoOutput
(
true
);
httpConn
.
setDoOutput
(
true
);
...
@@ -77,7 +77,7 @@ public class HttpHelper {
...
@@ -77,7 +77,7 @@ public class HttpHelper {
/**
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
*
* @param httpConn 链接信息
* @param httpConn
链接信息
* @param jsonString 请求参数,json字符串。
* @param jsonString 请求参数,json字符串。
* @return 远程响应结果
* @return 远程响应结果
*/
*/
...
@@ -88,7 +88,6 @@ public class HttpHelper {
...
@@ -88,7 +88,6 @@ public class HttpHelper {
BufferedReader
in
=
null
;
BufferedReader
in
=
null
;
PrintWriter
out
=
null
;
PrintWriter
out
=
null
;
// 处理请求参数
// 处理请求参数
// StringBuffer sb = new StringBuffer();
String
params
=
""
;
String
params
=
""
;
try
{
try
{
params
=
jsonString
;
params
=
jsonString
;
...
@@ -98,14 +97,10 @@ public class HttpHelper {
...
@@ -98,14 +97,10 @@ public class HttpHelper {
out
.
write
(
params
);
out
.
write
(
params
);
// flush输出流的缓冲
// flush输出流的缓冲
out
.
flush
();
out
.
flush
();
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
in
=
readStream
(
httpConn
.
getInputStream
(),
result
);
in
=
new
BufferedReader
(
new
InputStreamReader
(
httpConn
}
catch
(
Exception
ex
)
{
.
getInputStream
(),
"UTF-8"
));
in
=
readStream
(
httpConn
.
getErrorStream
(),
result
);
String
line
;
throw
new
RuntimeException
(
ex
.
getMessage
()
+
" 结果:"
+
result
.
toString
(),
ex
);
// 读取返回的内容
while
((
line
=
in
.
readLine
())
!=
null
)
{
result
.
append
(
line
);
}
}
finally
{
}
finally
{
if
(
out
!=
null
)
{
if
(
out
!=
null
)
{
out
.
close
();
out
.
close
();
...
@@ -117,6 +112,25 @@ public class HttpHelper {
...
@@ -117,6 +112,25 @@ public class HttpHelper {
return
result
.
toString
();
return
result
.
toString
();
}
}
/**
* 读取数据流
*
* @param stream
* @param result
* @return
* @throws IOException
*/
private
static
BufferedReader
readStream
(
InputStream
stream
,
StringBuilder
result
)
throws
IOException
{
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
stream
,
"UTF-8"
));
String
line
;
// 读取返回的内容
while
((
line
=
in
.
readLine
())
!=
null
)
{
result
.
append
(
line
);
}
return
in
;
}
/**
/**
* 下载文件
* 下载文件
*
*
...
...
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