----------httpclient-----------
```java
package util;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* HttpClient
*
* @author zpd 2018年3月29日
*/
public class HttpClientUtil {
private static final int timeOut = 30000;
/**
* POST-Form方式
*
* @param url
* @param jsonParam
* @return
* @throws Exception
*/
@SuppressWarnings("finally")
public static String postWithForm(String url, List<NameValuePair> param) {
String result = null;
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
httpclient = HttpClients.createDefault();
HttpPost postmethod = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut)
.setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
postmethod.setConfig(requestConfig);
postmethod.addHeader("content-type", "application/x-www-form-urlencoded");
postmethod.setEntity(new UrlEncodedFormEntity(param, Consts.UTF_8));
System.out.println("Executing request ------> " + postmethod.getRequestLine());
response = httpclient.execute(postmethod);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} else {
result = "{'code': '201','msg': 'fail','data': '接口请求失败!'}";
}
} catch (Exception e) {
result = "{'code': '201','msg': 'fail','data': '请求接口异常!'}";
} finally {
return result;
}
}
/**
* POST-JSON方式
*
* @param url
* @param jsonParam
* @return
* @throws Exception
*/
@SuppressWarnings("finally")
public static String postWithJSON(String url, String jsonParam) {
String result = null;
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
try {
httpclient = HttpClients.createDefault();
HttpPost postmethod = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut)
.setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
postmethod.setConfig(requestConfig);
postmethod.addHeader("content-type", "application/json");
postmethod.setEntity(new StringEntity(jsonParam, "UTF-8"));
response = httpclient.execute(postmethod);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} else {
result = "{'code': '201','msg': 'fail','data': '接口请求失败!'}";
}
} catch (Exception e) {
result = "{'code': '201','msg': 'fail','data': '请求接口异常!'}";
} finally {
return result;
}
}
}
----------httpclient-----------