/**
* POST请求
* @param url 请求地址
* @param data 请求数据
* @return 响应数据
* @throws Exception
*/
public String post(String url, String data) throws Exception{
HttpClient httpClient = null;
HttpPost httpPost = null;
HttpResponse response = null;
String result = "";
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(connectionTimeout)
.build();
httpPost.setConfig(requestConfig);
StringEntity dataEntity = new StringEntity(data, Charset.forName("UTF-8"));
httpPost.setEntity(dataEntity);
response = httpClient.execute(httpPost);
int retCode = response.getStatusLine().getStatusCode();
if (200 == retCode){
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
// 不超过2048M的内容可以转
int total = (int) entity.getContentLength();
byte[] bytes = new byte[total];
int readCount = 0;
int countTemp = 0;
if ((countTemp = instream.read(bytes, readCount, total)) != -1) {
readCount += countTemp;
}
result = new String(bytes, "UTF-8");
} else {
throw new Exception("请求失败,状态码为[" + retCode + "]");
}
} catch (Exception e) {
throw e;
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
return result;
}
maven文件
<groupId>demoapi</groupId>
<artifactId>demoapi</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>demoapi</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 基于httpcore的一套客户端 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<!-- http协议实现包 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<!-- 包含一些通用的编码解码算法。包括一些语音编码器,Hex,Base64 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- 通用日志接口 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 用于测试 -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<finalName>demoapi</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>