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
68bfd9d6
Commit
68bfd9d6
authored
Apr 26, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
不记录系统日志
parent
7cdd568b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
261 additions
and
10 deletions
+261
-10
HttpHelper.java
...src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+120
-10
HttpHelperTest.java
yzg-util-base/src/test/java/helper/HttpHelperTest.java
+50
-0
BaiduDate.java
yzg-util-base/src/test/java/helper/vo/BaiduDate.java
+91
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
View file @
68bfd9d6
...
@@ -4,11 +4,13 @@ package com.yanzuoguang.util.helper;
...
@@ -4,11 +4,13 @@ package com.yanzuoguang.util.helper;
import
com.yanzuoguang.util.exception.HttpCodeException
;
import
com.yanzuoguang.util.exception.HttpCodeException
;
import
com.yanzuoguang.util.thread.ProcessData
;
import
com.yanzuoguang.util.thread.ProcessData
;
import
com.yanzuoguang.util.thread.RunProcess
;
import
com.yanzuoguang.util.thread.RunProcess
;
import
com.yanzuoguang.util.vo.MapRow
;
import
java.io.*
;
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.net.URLConnection
;
import
java.util.Map
;
/**
/**
* HTTP请求工具类
* HTTP请求工具类
...
@@ -20,11 +22,15 @@ public class HttpHelper {
...
@@ -20,11 +22,15 @@ public class HttpHelper {
/**
/**
* 链接超时
* 链接超时
*/
*/
public
static
int
HTTP_CONN
N
ECT_TIMEOUT
=
10
*
1000
;
public
static
int
HTTP_CONNECT_TIMEOUT
=
10
*
1000
;
/**
/**
* 读取超时
* 读取超时
*/
*/
public
static
int
HTTP_READ_TIMEOUT
=
30
*
1000
;
public
static
int
HTTP_READ_TIMEOUT
=
30
*
1000
;
/**
* 字符编码
*/
public
static
final
String
CHARSET_DEFAULT
=
"UTF-8"
;
/**
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
...
@@ -37,7 +43,34 @@ public class HttpHelper {
...
@@ -37,7 +43,34 @@ public class HttpHelper {
try
{
try
{
// 打开URL连接
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
return
post
(
httpConn
,
jsonString
);
return
post
(
httpConn
,
jsonString
,
CHARSET_DEFAULT
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param url 目的地址
* @return 远程响应结果
*/
public
static
String
get
(
String
url
)
{
return
get
(
url
,
"UTF-8"
);
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param url 目的地址
* @return 远程响应结果
*/
public
static
String
get
(
String
url
,
String
charset
)
{
try
{
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
httpConn
.
setRequestMethod
(
"GET"
);
return
get
(
httpConn
,
charset
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
...
@@ -55,12 +88,42 @@ public class HttpHelper {
...
@@ -55,12 +88,42 @@ public class HttpHelper {
// 打开URL连接
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
httpConn
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
httpConn
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
return
post
(
httpConn
,
jsonString
);
return
post
(
httpConn
,
jsonString
,
"UTF-8"
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
}
}
/**
* 获取请求参数
*
* @param obj 对象
* @return
*/
public
static
String
getUrlParameter
(
Object
obj
)
{
return
getUrlParameter
(
obj
,
"UTF-8"
);
}
/**
* 获取请求参数
*
* @param obj 对象
* @return
*/
public
static
String
getUrlParameter
(
Object
obj
,
String
charset
)
{
MapRow
to
=
JsonHelper
.
to
(
obj
,
MapRow
.
class
);
StringBuilder
sb
=
new
StringBuilder
();
for
(
Map
.
Entry
<
String
,
Object
>
kvp
:
to
.
entrySet
())
{
if
(
sb
.
length
()
>
0
)
{
sb
.
append
(
"&"
);
}
sb
.
append
(
UrlHelper
.
encoding
(
kvp
.
getKey
(),
charset
));
sb
.
append
(
"="
);
sb
.
append
(
UrlHelper
.
encoding
(
StringHelper
.
toString
(
kvp
.
getValue
()),
charset
));
}
return
sb
.
toString
();
}
/**
/**
* 获取连接
* 获取连接
*
*
...
@@ -80,12 +143,11 @@ public class HttpHelper {
...
@@ -80,12 +143,11 @@ public class HttpHelper {
// 设置POST方式
// 设置POST方式
httpConn
.
setDoInput
(
true
);
httpConn
.
setDoInput
(
true
);
httpConn
.
setDoOutput
(
true
);
httpConn
.
setDoOutput
(
true
);
httpConn
.
setConnectTimeout
(
HTTP_CONN
N
ECT_TIMEOUT
);
httpConn
.
setConnectTimeout
(
HTTP_CONNECT_TIMEOUT
);
httpConn
.
setReadTimeout
(
HTTP_READ_TIMEOUT
);
httpConn
.
setReadTimeout
(
HTTP_READ_TIMEOUT
);
return
httpConn
;
return
httpConn
;
}
}
/**
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
*
...
@@ -94,6 +156,18 @@ public class HttpHelper {
...
@@ -94,6 +156,18 @@ public class HttpHelper {
* @return 远程响应结果
* @return 远程响应结果
*/
*/
public
static
String
post
(
HttpURLConnection
httpConn
,
String
jsonString
)
throws
IOException
{
public
static
String
post
(
HttpURLConnection
httpConn
,
String
jsonString
)
throws
IOException
{
return
post
(
httpConn
,
jsonString
,
CHARSET_DEFAULT
);
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param httpConn 链接信息
* @param jsonString 请求参数,json字符串。
* @param charset 输出编码
* @return 远程响应结果
*/
public
static
String
post
(
HttpURLConnection
httpConn
,
String
jsonString
,
String
charset
)
throws
IOException
{
// 返回的结果
// 返回的结果
StringBuilder
result
=
new
StringBuilder
();
StringBuilder
result
=
new
StringBuilder
();
// 读取响应输入流
// 读取响应输入流
...
@@ -109,9 +183,10 @@ public class HttpHelper {
...
@@ -109,9 +183,10 @@ public class HttpHelper {
out
.
write
(
params
);
out
.
write
(
params
);
// flush输出流的缓冲
// flush输出流的缓冲
out
.
flush
();
out
.
flush
();
in
=
readStream
(
httpConn
.
getInputStream
(),
result
);
in
=
readStream
(
httpConn
.
getInputStream
(),
result
,
charset
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
in
=
readStream
(
httpConn
.
getErrorStream
(),
result
);
in
=
readStream
(
httpConn
.
getErrorStream
(),
result
,
charset
);
throw
new
HttpCodeException
(
StringHelper
.
toString
(
httpConn
.
getResponseCode
()),
ex
.
getMessage
(),
result
.
toString
(),
ex
);
throw
new
HttpCodeException
(
StringHelper
.
toString
(
httpConn
.
getResponseCode
()),
ex
.
getMessage
(),
result
.
toString
(),
ex
);
}
finally
{
}
finally
{
if
(
out
!=
null
)
{
if
(
out
!=
null
)
{
...
@@ -124,6 +199,41 @@ public class HttpHelper {
...
@@ -124,6 +199,41 @@ public class HttpHelper {
return
result
.
toString
();
return
result
.
toString
();
}
}
/**
* 发送GET请求,当请求失败时,抛出异常或返回空字符串
*
* @param httpConn 链接信息
* @return 远程响应结果
*/
public
static
String
get
(
HttpURLConnection
httpConn
)
throws
IOException
{
return
get
(
httpConn
,
CHARSET_DEFAULT
);
}
/**
* 发送GET请求,当请求失败时,抛出异常或返回空字符串
*
* @param httpConn 链接信息
* @return 远程响应结果
*/
public
static
String
get
(
HttpURLConnection
httpConn
,
String
charset
)
throws
IOException
{
// 返回的结果
StringBuilder
result
=
new
StringBuilder
();
// 读取响应输入流
BufferedReader
in
=
null
;
try
{
// 获取HttpURLConnection对象对应的输出流
in
=
readStream
(
httpConn
.
getInputStream
(),
result
,
charset
);
}
catch
(
Exception
ex
)
{
in
=
readStream
(
httpConn
.
getErrorStream
(),
result
,
charset
);
throw
new
HttpCodeException
(
StringHelper
.
toString
(
httpConn
.
getResponseCode
()),
ex
.
getMessage
(),
result
.
toString
(),
ex
);
}
finally
{
if
(
in
!=
null
)
{
in
.
close
();
}
}
return
result
.
toString
();
}
/**
/**
* 读取数据流
* 读取数据流
*
*
...
@@ -132,9 +242,9 @@ public class HttpHelper {
...
@@ -132,9 +242,9 @@ public class HttpHelper {
* @return
* @return
* @throws IOException
* @throws IOException
*/
*/
private
static
BufferedReader
readStream
(
InputStream
stream
,
StringBuilder
result
)
throws
IOException
{
private
static
BufferedReader
readStream
(
InputStream
stream
,
StringBuilder
result
,
String
charset
)
throws
IOException
{
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
stream
,
"UTF-8"
));
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
stream
,
charset
));
String
line
;
String
line
;
// 读取返回的内容
// 读取返回的内容
while
((
line
=
in
.
readLine
())
!=
null
)
{
while
((
line
=
in
.
readLine
())
!=
null
)
{
...
@@ -166,7 +276,7 @@ public class HttpHelper {
...
@@ -166,7 +276,7 @@ public class HttpHelper {
int
byteRead
;
int
byteRead
;
URL
url
=
new
URL
(
serverFileName
);
URL
url
=
new
URL
(
serverFileName
);
URLConnection
conn
=
url
.
openConnection
();
URLConnection
conn
=
url
.
openConnection
();
conn
.
setConnectTimeout
(
HTTP_CONN
N
ECT_TIMEOUT
);
conn
.
setConnectTimeout
(
HTTP_CONNECT_TIMEOUT
);
conn
.
setReadTimeout
(
HTTP_READ_TIMEOUT
);
conn
.
setReadTimeout
(
HTTP_READ_TIMEOUT
);
InputStream
inStream
=
conn
.
getInputStream
();
InputStream
inStream
=
conn
.
getInputStream
();
try
{
try
{
...
...
yzg-util-base/src/test/java/helper/HttpHelperTest.java
0 → 100644
View file @
68bfd9d6
package
helper
;
import
com.yanzuoguang.util.helper.HttpHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
helper.vo.BaiduDate
;
import
org.junit.Test
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
public
class
HttpHelperTest
{
@Test
public
void
test
()
{
long
time
=
System
.
currentTimeMillis
();
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"query"
,
"2021年10月"
);
map
.
put
(
"co"
,
""
);
map
.
put
(
"resource_id"
,
"39043"
);
map
.
put
(
"t"
,
time
);
map
.
put
(
"ie"
,
"utf8"
);
map
.
put
(
"oe"
,
"gbk"
);
map
.
put
(
"cb"
,
"op_aladdin_callback"
);
map
.
put
(
"format"
,
"json"
);
map
.
put
(
"tn"
,
"wisetpl"
);
map
.
put
(
"cb"
,
"jQuery1102007795278844876075_"
+
(
time
-
30
));
map
.
put
(
"_"
,
time
-
50
);
String
para
=
HttpHelper
.
getUrlParameter
(
map
);
String
url
=
"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?"
;
String
fullUrl
=
url
+
para
;
String
post
=
HttpHelper
.
get
(
fullUrl
,
"gbk"
);
System
.
out
.
println
(
post
);
Pattern
compile
=
Pattern
.
compile
(
".*\\((.*)\\).*"
);
Matcher
matcher
=
compile
.
matcher
(
post
);
if
(
matcher
.
find
())
{
String
group
=
matcher
.
group
(
1
);
System
.
out
.
println
(
group
);
BaiduDate
result
=
JsonHelper
.
deserialize
(
group
,
BaiduDate
.
class
);
System
.
out
.
println
(
result
.
getStatus
());
}
}
}
yzg-util-base/src/test/java/helper/vo/BaiduDate.java
0 → 100644
View file @
68bfd9d6
package
helper
.
vo
;
import
java.util.List
;
public
class
BaiduDate
{
public
static
final
int
version
=
143
;
private
String
status
;
private
List
<
BaiduData
>
data
;
public
String
getStatus
()
{
return
status
;
}
public
void
setStatus
(
String
status
)
{
this
.
status
=
status
;
}
public
List
<
BaiduData
>
getData
()
{
return
data
;
}
public
void
setData
(
List
<
BaiduData
>
data
)
{
this
.
data
=
data
;
}
public
static
final
class
BaiduData
{
private
int
_version
;
public
int
get_version
()
{
return
_version
;
}
public
void
set_version
(
int
_version
)
{
this
.
_version
=
_version
;
}
private
List
<
BaiduUlmanac
>
almanac
;
public
List
<
BaiduUlmanac
>
getAlmanac
()
{
return
almanac
;
}
public
void
setAlmanac
(
List
<
BaiduUlmanac
>
almanac
)
{
this
.
almanac
=
almanac
;
}
}
public
static
final
class
BaiduUlmanac
{
// 牛
public
String
animal
;
// 装修.动土.安床.出行.安葬.上梁.旅游.破土.修造.求医.竖柱.词讼.出师.打官司
public
String
avoid
;
// 二
public
String
cnDay
;
// 14
public
String
day
;
// 乙丑
public
String
gzDate
;
// 丁酉
public
String
gzMonth
;
// 辛丑
public
String
gzYear
;
// 1 or ""
public
String
isBigMonth
;
// 初八
public
String
lDate
;
// 八
public
String
lMonth
;
// 8
public
String
lunarDate
;
// 8
public
String
lunarMonth
;
// 2021
public
String
lunarYear
;
// 9
public
String
month
;
// 2021
public
String
oDate
;
// 搬家.开业.结婚.入宅.领证.开工.订婚.开张.作灶.入学.求嗣.赴任.祈福.祭祀.开市.纳财.纳畜.裁衣.嫁娶.纳采.移徙.盖屋.冠笄.栽种.斋醮.求财.招赘.纳婿
public
String
suit
;
// 24节气
public
String
term
;
// 2021
public
String
year
;
}
}
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