一、获取jar
实现任何功能肯定是先获取相应工具类,即jar包。可以使用复制的方式或者maven获取。项目中已导入jar,可以直接使用。
需要的jar入图:

项目地址:svn://www.kittybunny.cn/kitty/2%E3%80%81code/httpdemo 用户名密码:reader/reader
maven地址:
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
<!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency>
|
二、编写工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| package club.kittybunny.httpclient;
import java.util.HashMap; import java.util.Map;
import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; 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;
import com.alibaba.fastjson.JSON; /** * 除了使用项目提供的jar也可以使用maven下载,参考地址如下 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
<!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.32</version> </dependency>
* @author bunny~~我是兔子我会喵,我叫喵星兔。 * */ public class HttpUtil { /** 请求编码 */ private static final String DEFAULT_CHARSET = "UTF-8";
/** * 执行HTTP POST请求 * * @param url * url * @param param * 参数 * @return */ public static String httpPostWithJSON(String url, Map<String, ?> param) { CloseableHttpClient client = null; try { if (url == null || url.trim().length() == 0) { throw new Exception("URL is null"); } HttpPost httpPost = new HttpPost(url); client = HttpClients.createDefault(); if (param != null) { StringEntity entity = new StringEntity(JSON.toJSONString(param), DEFAULT_CHARSET);// 解决中文乱码问题 entity.setContentEncoding(DEFAULT_CHARSET); entity.setContentType("application/json"); httpPost.setEntity(entity); } HttpResponse resp = client.execute(httpPost); if (resp.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET); } } catch (Exception e) { e.printStackTrace(); } finally { close(client); } return null; }
/** * 执行HTTP GET请求 * * @param url * url * @param param * 参数 * @return */ public static String httpGetWithJSON(String url, Map<String, ?> param) { CloseableHttpClient client = null; try { if (url == null || url.trim().length() == 0) { throw new Exception("URL is null"); } client = HttpClients.createDefault(); if (param != null) { StringBuffer sb = new StringBuffer("?"); for (String key : param.keySet()) { sb.append(key).append("=").append(param.get(key)).append("&"); } url = url.concat(sb.toString()); url = url.substring(0, url.length() - 1); } HttpGet httpGet = new HttpGet(url); HttpResponse resp = client.execute(httpGet); if (resp.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET); } } catch (Exception e) { e.printStackTrace(); } finally { close(client); } return null; }
/** * 关闭HTTP请求 * * @param client */ private static void close(CloseableHttpClient client) { if (client == null) { return; } try { client.close(); } catch (Exception e) { } } }
|
三、编写测试类
1 2 3 4 5 6 7 8
| public static void main(String[] args) { Map<String, String> param = new HashMap<>(); param.put("name", "hehe");
String result = httpGetWithJSON("http://127.0.0.1:9999/hello", param); System.out.println("result:" + result); }
|
执行结果如下:
