|
| 1 | +package java0.nio01.httpclient; |
| 2 | +import java.util.Map; |
| 3 | + |
| 4 | +import org.apache.http.HttpEntity; |
| 5 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 6 | +import org.apache.http.client.methods.HttpGet; |
| 7 | +import org.apache.http.client.methods.HttpPost; |
| 8 | +import org.apache.http.entity.ContentType; |
| 9 | +import org.apache.http.entity.StringEntity; |
| 10 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 11 | +import org.apache.http.impl.client.HttpClients; |
| 12 | +import org.apache.http.util.EntityUtils; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * http客户端 |
| 17 | + * |
| 18 | + */ |
| 19 | +public class HttpClient { |
| 20 | + |
| 21 | + /** |
| 22 | + * post请求传输json数据 |
| 23 | + * |
| 24 | + * @param url url地址 |
| 25 | + * @param json json数据 |
| 26 | + * @param encoding 编码方式 |
| 27 | + * @param headerMap 请求头中的键值对 |
| 28 | + * @return |
| 29 | + * @throws Exception |
| 30 | + */ |
| 31 | + public static String sendPostDataByJson(String url, String json, String encoding, Map<String, String> headerMap) throws Exception { |
| 32 | + String result = ""; |
| 33 | + |
| 34 | + // 创建httpclient对象 |
| 35 | + CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 36 | + // 创建post方式请求对象 |
| 37 | + HttpPost httpPost = new HttpPost(url); |
| 38 | + // 将头信息设置到请求对象中 |
| 39 | + if(headerMap != null) { |
| 40 | + headerMap.forEach((key, value) -> { |
| 41 | + httpPost.addHeader(key, value); |
| 42 | + }); |
| 43 | + } |
| 44 | + // 设置参数到请求对象中 |
| 45 | + StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON); |
| 46 | + stringEntity.setContentEncoding(encoding); |
| 47 | + httpPost.setEntity(stringEntity); |
| 48 | + // 执行请求操作,并拿到结果(同步阻塞) |
| 49 | + CloseableHttpResponse response = httpClient.execute(httpPost); |
| 50 | + // 获取结果实体 |
| 51 | + // 判断网络连接状态码是否正常(0--200都数正常) |
| 52 | + // response.getStatusLine().getStatusCode() == HttpStatus.SC_OK |
| 53 | + result = EntityUtils.toString(response.getEntity(), "utf-8"); |
| 54 | + // 释放链接 |
| 55 | + response.close(); |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + public static String sendPostDataByJson(String url, String json) throws Exception { |
| 60 | + return sendPostDataByJson(url, json, "utf-8", null); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * post请求传输Xml数据 |
| 65 | + * |
| 66 | + * @param url url地址 |
| 67 | + * @param xml xml数据 |
| 68 | + * @param encoding 编码方式 |
| 69 | + * @return |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + public static String sendPostDataByXml(String url, String xml, String encoding, Map<String, String> headerMap) throws Exception { |
| 73 | + String result = ""; |
| 74 | + // 创建httpclient对象 |
| 75 | + CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 76 | + // 创建post方式请求对象 |
| 77 | + HttpPost httpPost = new HttpPost(url); |
| 78 | + if(headerMap != null) { |
| 79 | + headerMap.forEach((key, value) -> { |
| 80 | + httpPost.addHeader(key, value); |
| 81 | + }); |
| 82 | + } |
| 83 | + // 设置参数到请求对象中 |
| 84 | + StringEntity stringEntity = new StringEntity(xml, ContentType.APPLICATION_XML); |
| 85 | + stringEntity.setContentEncoding(encoding); |
| 86 | + httpPost.setEntity(stringEntity); |
| 87 | + // 执行请求操作,并拿到结果(同步阻塞) |
| 88 | + CloseableHttpResponse response = httpClient.execute(httpPost); |
| 89 | + // 获取结果实体 |
| 90 | + // 判断网络连接状态码是否正常(0--200都数正常) |
| 91 | + // response.getStatusLine().getStatusCode() == HttpStatus.SC_OK |
| 92 | + result = EntityUtils.toString(response.getEntity(), encoding); |
| 93 | + // 释放链接 |
| 94 | + response.close(); |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + public static String sendPostDataByXml(String url, String xml) throws Exception { |
| 99 | + return sendPostDataByXml(url, xml, "GBK", null); |
| 100 | + } |
| 101 | + |
| 102 | + // GET 调用 |
| 103 | + public static String getAsString(String url) throws Exception{ |
| 104 | + CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 105 | + HttpGet httpGet = new HttpGet(url); |
| 106 | + CloseableHttpResponse response1 = null; |
| 107 | + try { |
| 108 | + response1 = httpClient.execute(httpGet); |
| 109 | + System.out.println(response1.getStatusLine()); |
| 110 | + HttpEntity entity1 = response1.getEntity(); |
E839
| 111 | + return EntityUtils.toString(entity1, "UTF-8"); |
| 112 | + } finally { |
| 113 | + if (null != response1) { |
| 114 | + response1.close(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static void main(String[] args) throws Exception { |
| 120 | + System.out.println(getAsString("http://localhost:8088/api/hello")); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments