pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<groupId>**</groupId>
<artifactId>**</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>userReplace</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Demo
public static JSONObject doGet(String url,HashMap<String,String> header) throws IOException {
GetMethod getMethod = new GetMethod(url);
header.forEach((key,value) -> getMethod.setRequestHeader(key,value));
HttpClient httpClient = new HttpClient();
int i = httpClient.executeMethod(getMethod);
JSONObject response = null;
response.put("code",i);
response.put("response",JSONObject.parseObject(inputStreamToString(getMethod.getResponseBodyAsStream())));
return response;
}
public static JSONObject doPost(String url,HashMap<String,String> header,ArrayList<Object> params) throws IOException {
PostMethod postMethod = new PostMethod(url);
RequestEntity requestEntity = new StringRequestEntity(JSONArray.toJSONString(params), "application/json", "UTF-8");
postMethod.setRequestEntity(requestEntity);
header.forEach((key,value) -> postMethod.setRequestHeader(key,value));
HttpClient httpClient = new HttpClient();
int i = httpClient.executeMethod(postMethod);
JSONObject response = null;
response.put("code",i);
response.put("response",JSONObject.parseObject(inputStreamToString(postMethod.getResponseBodyAsStream())));
return response;
}
public static String inputStreamToString(InputStream inputStream){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
byte[] b = new byte[10240];
int n;
while ((n = inputStream.read(b)) != -1) {
outputStream.write(b, 0, n);
}
} catch (Exception e) {
try {
inputStream.close();
outputStream.close();
} catch (Exception e1) {
logger.error("gav4媒体数据拉取结果转换异常");
}
}
return outputStream.toString();
}