在pom.xml增加httpclient
<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 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>json</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>json</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.6</version></dependency></dependencies></project>
封装操作类
package com.example;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import java.util.ArrayList;import java.util.List;import java.util.Map;/*** HttpClient工具类*/public class HttpUtil {//private static Logger logger = Logger.getLogger(HttpUtil.class);/*** get请求* @return*/public static String get(String url,Map<String,String>headers){String result = "";try {// 构造httprequest设置RequestConfig config = RequestConfig.custom().setConnectTimeout(5).setConnectionRequestTimeout(10).build();HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();HttpGet htGet = new HttpGet(url);// 添加http headersif (headers != null && headers.size() > 0) {for (String key : headers.keySet()) {htGet.addHeader(key, headers.get(key));}}// 读取数据HttpResponse response = client.execute(htGet);result = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {System.out.println("发送GET请求出现异常!" + e);e.printStackTrace();}return result;}/*不带参数请求*/public static String get(String url) {return get(url,null);}/*** post请求(用于key-value格式的参数)* @param url* @param param* @return*/public static String post(String url, Map<String, String> param) {return post(url,param,null);}public static String post(String url,Map<String,String> param,Map<String,String> headers){String result="";try {// 构造httprequest设置RequestConfig config = RequestConfig.custom().setConnectTimeout(5).setConnectionRequestTimeout(10).build();HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();HttpPost httpPost = new HttpPost(url);if (param != null) {List<NameValuePair> paramList = new ArrayList<NameValuePair>();for (String key : param.keySet()) {paramList.add(new BasicNameValuePair(key, param.get(key)));}// 模拟表单UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");httpPost.setEntity(entity);}// 添加http headersif (headers != null && headers.size() > 0) {for (String key : headers.keySet()) {httpPost.addHeader(key, headers.get(key));}}// 读取数据HttpResponse response = client.execute(httpPost);result = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {System.out.println("发送GET请求出现异常!" + e);e.printStackTrace();}return result;}}
