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
0bbecbd1
Commit
0bbecbd1
authored
Jan 13, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
下载视频
parent
fb329a78
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
56 deletions
+101
-56
HttpHelper.java
...src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+101
-56
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
View file @
0bbecbd1
...
...
@@ -32,6 +32,106 @@ public class HttpHelper {
*/
public
static
final
String
CHARSET_DEFAULT
=
"UTF-8"
;
/**
* 获取请求参数
*
* @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
();
}
/**
* 获取连接
*
* @param url 请求地址
* @return
* @throws IOException
*/
public
static
HttpURLConnection
getConn
(
String
url
)
throws
IOException
{
return
getConn
(
url
,
null
,
false
);
}
/**
* 获取连接
*
* @param url 请求地址
* @param header 请求头
* @return
* @throws IOException
*/
public
static
HttpURLConnection
getConn
(
String
url
,
Map
<
String
,
String
>
header
)
throws
IOException
{
return
getConn
(
url
,
header
,
false
);
}
/**
* 获取连接
*
* @param url 请求地址
* @param isApplicationJson 是否请求json
* @return
* @throws IOException
*/
public
static
HttpURLConnection
getConn
(
String
url
,
boolean
isApplicationJson
)
throws
IOException
{
return
getConn
(
url
,
null
,
isApplicationJson
);
}
/**
* 获取连接
*
* @param url 请求地址
* @param header 请求头
* @param isApplicationJson 是否请求json
* @return
* @throws IOException
*/
public
static
HttpURLConnection
getConn
(
String
url
,
Map
<
String
,
String
>
header
,
boolean
isApplicationJson
)
throws
IOException
{
// 创建URL对象
java
.
net
.
URL
connURL
=
new
java
.
net
.
URL
(
url
);
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
(
java
.
net
.
HttpURLConnection
)
connURL
.
openConnection
();
// 设置通用属性
httpConn
.
setRequestProperty
(
"Accept"
,
"*/*"
);
httpConn
.
setRequestProperty
(
"Connection"
,
"Keep-Alive"
);
httpConn
.
setRequestProperty
(
"User-Agent"
,
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
);
if
(
isApplicationJson
)
{
httpConn
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
}
// 处理自定义头
if
(
header
!=
null
)
{
for
(
Map
.
Entry
<
String
,
String
>
item
:
header
.
entrySet
())
{
httpConn
.
setRequestProperty
(
item
.
getKey
(),
item
.
getValue
());
}
}
// 设置POST方式
httpConn
.
setDoInput
(
true
);
httpConn
.
setDoOutput
(
true
);
httpConn
.
setConnectTimeout
(
HTTP_CONNECT_TIMEOUT
);
httpConn
.
setReadTimeout
(
HTTP_READ_TIMEOUT
);
return
httpConn
;
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
...
...
@@ -86,68 +186,13 @@ public class HttpHelper {
public
static
String
postApplicationJSON
(
String
url
,
String
jsonString
)
{
try
{
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
);
httpConn
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
,
true
);
return
post
(
httpConn
,
jsonString
,
"UTF-8"
);
}
catch
(
Exception
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
();
}
/**
* 获取连接
*
* @param url
* @return
* @throws IOException
*/
private
static
HttpURLConnection
getConn
(
String
url
)
throws
IOException
{
// 创建URL对象
java
.
net
.
URL
connURL
=
new
java
.
net
.
URL
(
url
);
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
(
java
.
net
.
HttpURLConnection
)
connURL
.
openConnection
();
// 设置通用属性
httpConn
.
setRequestProperty
(
"Accept"
,
"*/*"
);
httpConn
.
setRequestProperty
(
"Connection"
,
"Keep-Alive"
);
httpConn
.
setRequestProperty
(
"User-Agent"
,
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"
);
// 设置POST方式
httpConn
.
setDoInput
(
true
);
httpConn
.
setDoOutput
(
true
);
httpConn
.
setConnectTimeout
(
HTTP_CONNECT_TIMEOUT
);
httpConn
.
setReadTimeout
(
HTTP_READ_TIMEOUT
);
return
httpConn
;
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
...
...
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