Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
goodsinfo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bigdata
goodsinfo
Commits
b6e355cb
Commit
b6e355cb
authored
May 26, 2021
by
杨林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
请求网络工具
parent
0457b7f3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
2 deletions
+22
-2
src/main/scala/qm/tools/http/HttpTools.scala
src/main/scala/qm/tools/http/HttpTools.scala
+22
-2
No files found.
src/main/scala/qm/tools/http/HttpTools.scala
View file @
b6e355cb
package
qm.tools.http
import
java.util.Properties
import
com.alibaba.fastjson.JSON
import
org.apache.http.client.methods.
{
HttpGet
,
HttpPost
,
HttpRequestBase
}
import
org.apache.http.entity.StringEntity
import
org.apache.http.impl.client.
{
CloseableHttpClient
,
HttpClients
}
import
org.apache.http.util.EntityUtils
import
qm.tools.json.Jackson
import
qm.tools.properties.PropertiesTools
import
qm.tools.unicode.UnicodeUtils
import
scala.collection.mutable
...
...
@@ -13,13 +21,15 @@ import scala.collection.mutable
* @Time: 11:25
*/
object
HttpTools
{
private
[
this
]
val
properties
:
Properties
=
PropertiesTools
.
getProperties
def
apply
(
method
:
String
,
url
:
String
,
header
:
String
)
:
HttpTools
=
new
HttpTools
(
method
,
url
,
header
)
}
class
HttpTools
(
method
:
String
,
url
:
String
,
header
:
String
=
"""
|{"Content-Type":"application/json"}
|"""
.
stripMargin
)
{
private
[
this
]
val
httpClient
:
CloseableHttpClient
=
HttpClients
.
createDefault
()
var
httpRequest
:
HttpRequestBase
=
_
...
...
@@ -38,7 +48,17 @@ class HttpTools(method: String, url: String, header: String =
if
(
params
!=
null
)
{
val
body
:
mutable.Map
[
String
,
String
]
=
mutable
.
Map
()
body
+=
(
"text"
->
params
)
val
str
=
Jackson
.
parseBeanToString
(
body
)
httpRequest
.
asInstanceOf
[
HttpPost
].
setEntity
(
new
StringEntity
(
str
,
"utf-8"
))
}
val
response
=
httpClient
.
execute
(
httpRequest
)
val
str
=
EntityUtils
.
toString
(
response
.
getEntity
,
"utf-8"
)
val
map
=
Jackson
.
parseFirstKey
(
UnicodeUtils
.
decode
(
str
))
val
code
=
map
.
getOrElse
(
"code"
,
"0"
).
toInt
val
msg
=
map
.
getOrElse
(
"msg"
,
""
)
if
(
code
==
200
&&
msg
.
equals
(
"success"
))
{
map
.
getOrElse
(
"data"
,
""
)
}
else
""
}
}
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