Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
sap-service
Project
Project
Details
Activity
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
曲欣亮
sap-service
Commits
687cbae3
Commit
687cbae3
authored
Aug 05, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
c20aa7c6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
0 deletions
+42
-0
HttpUtil.java
src/main/java/com/egolm/sso/util/HttpUtil.java
+42
-0
No files found.
src/main/java/com/egolm/sso/util/HttpUtil.java
View file @
687cbae3
package
com
.
egolm
.
sso
.
util
;
package
com
.
egolm
.
sso
.
util
;
import
java.io.OutputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.net.HttpURLConnection
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URL
;
...
@@ -10,6 +11,8 @@ import java.util.Map;
...
@@ -10,6 +11,8 @@ import java.util.Map;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
import
com.alibaba.fastjson.JSON
;
public
class
HttpUtil
{
public
class
HttpUtil
{
public
static
String
get
(
String
requestUrl
,
Map
<
String
,
String
>
headers
)
{
public
static
String
get
(
String
requestUrl
,
Map
<
String
,
String
>
headers
)
{
...
@@ -77,6 +80,36 @@ public class HttpUtil {
...
@@ -77,6 +80,36 @@ public class HttpUtil {
try
{
try
{
String
requestBody
=
toQueryString
(
parameters
);
String
requestBody
=
toQueryString
(
parameters
);
requestUrl
+=
(
requestUrl
.
contains
(
"?"
)
?
"&"
:
"?"
)
+
requestBody
;
requestUrl
+=
(
requestUrl
.
contains
(
"?"
)
?
"&"
:
"?"
)
+
requestBody
;
System
.
out
.
println
(
requestUrl
);
System
.
out
.
println
(
JSON
.
toJSONString
(
parameters
));
System
.
out
.
println
(
JSON
.
toJSONString
(
headers
));
URL
getUrl
=
new
URL
(
requestUrl
);
connection
=
(
HttpURLConnection
)
getUrl
.
openConnection
();
connection
.
setRequestMethod
(
"POST"
);
connection
.
setRequestProperty
(
"Accept-Charset"
,
"utf-8"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
if
(
headers
!=
null
)
{
for
(
String
key
:
headers
.
keySet
())
{
connection
.
setRequestProperty
(
key
,
headers
.
get
(
key
));
}
}
connection
.
connect
();
return
responseBody
(
connection
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
finally
{
connection
.
disconnect
();
}
}
public
static
String
postRaw
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
headers
)
{
HttpURLConnection
connection
=
null
;
OutputStream
os
=
null
;
try
{
System
.
out
.
println
(
requestUrl
);
System
.
out
.
println
(
JSON
.
toJSONString
(
parameters
));
System
.
out
.
println
(
JSON
.
toJSONString
(
headers
));
URL
getUrl
=
new
URL
(
requestUrl
);
URL
getUrl
=
new
URL
(
requestUrl
);
connection
=
(
HttpURLConnection
)
getUrl
.
openConnection
();
connection
=
(
HttpURLConnection
)
getUrl
.
openConnection
();
connection
.
setRequestMethod
(
"POST"
);
connection
.
setRequestMethod
(
"POST"
);
...
@@ -88,10 +121,19 @@ public class HttpUtil {
...
@@ -88,10 +121,19 @@ public class HttpUtil {
}
}
}
}
connection
.
connect
();
connection
.
connect
();
os
=
connection
.
getOutputStream
();
String
rawData
=
JSON
.
toJSONString
(
parameters
);
os
.
write
(
rawData
.
getBytes
());
os
.
flush
();
return
responseBody
(
connection
);
return
responseBody
(
connection
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
}
finally
{
try
{
os
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
connection
.
disconnect
();
connection
.
disconnect
();
}
}
}
}
...
...
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