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
d4266509
Commit
d4266509
authored
Feb 22, 2023
by
yanzg
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
4fcecdf6
c743315f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
0 deletions
+47
-0
pom.xml
yzg-util-base/pom.xml
+4
-0
HttpHelper.java
...src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+43
-0
No files found.
yzg-util-base/pom.xml
View file @
d4266509
...
...
@@ -25,6 +25,10 @@
<groupId>
io.springfox
</groupId>
<artifactId>
springfox-swagger2
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
</dependency>
</dependencies>
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
View file @
d4266509
...
...
@@ -5,11 +5,21 @@ import com.yanzuoguang.util.exception.HttpCodeException;
import
com.yanzuoguang.util.thread.ProcessData
;
import
com.yanzuoguang.util.thread.RunProcess
;
import
com.yanzuoguang.util.vo.MapRow
;
import
org.apache.commons.codec.Charsets
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.NameValuePair
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.apache.http.util.EntityUtils
;
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.util.List
;
import
java.util.Map
;
/**
...
...
@@ -414,5 +424,38 @@ public class HttpHelper {
}
}
/**
* post请求form-data
* @param url
* @param parametersBody
* @return
* @throws Exception
*/
public
static
String
postRequest
(
String
url
,
List
<
NameValuePair
>
parametersBody
)
throws
Exception
{
HttpEntity
entity
=
new
UrlEncodedFormEntity
(
parametersBody
,
Charsets
.
UTF_8
);
return
post
(
url
,
"application/x-www-form-urlencoded"
,
entity
);
}
private
static
String
post
(
String
url
,
String
mediaType
,
HttpEntity
entity
)
throws
Exception
{
HttpPost
post
=
new
HttpPost
(
url
);
post
.
addHeader
(
"Content-Type"
,
mediaType
);
post
.
addHeader
(
"Accept"
,
"application/json"
);
post
.
setEntity
(
entity
);
String
var8
;
try
{
HttpClient
client
=
HttpClientBuilder
.
create
().
build
();
HttpResponse
response
=
client
.
execute
(
post
);
int
code
=
response
.
getStatusLine
().
getStatusCode
();
if
(
code
>=
400
)
{
throw
new
Exception
(
EntityUtils
.
toString
(
response
.
getEntity
()));
}
var8
=
EntityUtils
.
toString
(
response
.
getEntity
());
}
catch
(
Exception
var11
)
{
throw
new
Exception
(
"postRequest -- Client protocol exception!"
,
var11
);
}
finally
{
post
.
releaseConnection
();
}
return
var8
;
}
}
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