|
| 1 | +package com.azxx.demon.utils; |
| 2 | + |
| 3 | +import org.apache.http.HttpEntity; |
| 4 | +import org.apache.http.client.HttpClient; |
| 5 | +import org.apache.http.client.config.RequestConfig; |
| 6 | +import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 7 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 8 | +import org.apache.http.client.methods.HttpGet; |
| 9 | +import org.apache.http.client.methods.HttpPost; |
| 10 | +import org.apache.http.client.methods.HttpUriRequest; |
| 11 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 12 | +import org.apache.http.impl.client.HttpClients; |
| 13 | +import org.apache.http.message.BasicNameValuePair; |
| 14 | +import org.apache.http.util.EntityUtils; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +public class HttpUtils { |
| 24 | + |
| 25 | + private static Logger logger = LoggerFactory.getLogger(HttpUtils.class); |
| 26 | + |
| 27 | + public static String httpGet(String url, Map<String, String> params) { |
| 28 | + |
| 29 | + StringBuffer sb = new StringBuffer(); |
| 30 | + if (params != null) { |
| 31 | + for (Map.Entry<String, String> entry : params.entrySet()) { |
| 32 | + String param = entry.getKey() + "=" + entry.getValue(); |
| 33 | + sb.append(param + "&"); |
| 34 | + } |
| 35 | + url = url+"?"+sb.toString(); |
| 36 | + } |
| 37 | + //1.创建HttpClient |
| 38 | + CloseableHttpClient client = HttpClients.createDefault(); |
| 39 | + //2.创建HttpGet |
| 40 | + HttpGet httpGet = new HttpGet(url ); |
| 41 | + CloseableHttpResponse response = null; |
| 42 | + String content = null; |
| 43 | + |
| 44 | + try { |
| 45 | + //3.执行Get请求 |
| 46 | + response = client.execute(httpGet); |
| 47 | + //4.获取响应实体 |
| 48 | + HttpEntity entity = response.getEntity(); |
| 49 | + //获取返回状态码 |
| 50 | + logger.info(String.valueOf(response.getStatusLine())); |
| 51 | + if (entity != null) { |
| 52 | + content = EntityUtils.toString(entity); |
| 53 | + logger.info(content); |
| 54 | + } |
| 55 | + |
| 56 | + } catch (IOException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } finally { |
| 59 | + try { |
| 60 | + response.close(); |
| 61 | + client.close(); |
| 62 | + } catch (IOException e) { |
| 63 | + e.printStackTrace(); |
| 64 | + } |
| 65 | + } |
| 66 | + return content; |
| 67 | + } |
| 68 | + |
| 69 | + public static String httpPost(String url, Map<String, String> params) { |
| 70 | + |
| 71 | + //1.创建HttpClient |
| 72 | + CloseableHttpClient client = HttpClients.createDefault(); |
| 73 | + //2.创建HttpGet |
| 74 | + HttpPost httpPost = new HttpPost(url); |
| 75 | + CloseableHttpResponse response = null; |
| 76 | + String content = null; |
| 77 | + //设置请求和传输超时时间 |
| 78 | + RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(6000).build(); |
| 79 | + httpPost.setConfig(requestConfig); |
| 80 | + httpPost.addHeader("User-Agent", "Mozilla/5.0"); |
| 81 | + // 创建参数队列 |
| 82 | + List formparams = new ArrayList(); |
| 83 | + for (Map.Entry<String, String> entry : params.entrySet()) { |
| 84 | + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); |
| 85 | + } |
| 86 | + |
| 87 | + UrlEncodedFormEntity uefEntity; |
| 88 | + try { |
| 89 | + uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); |
| 90 | + httpPost.setEntity(uefEntity); |
| 91 | + //3.执行Get请求 |
| 92 | + response = client.execute(httpPost); |
| 93 | + //4.获取响应实体 |
| 94 | + HttpEntity entity = response.getEntity(); |
| 95 | + //获取返回状态码 |
| 96 | + logger.info(String.valueOf(response.getStatusLine())); |
| 97 | + if (entity != null) { |
| 98 | + content = EntityUtils.toString(entity); |
| 99 | + logger.info(content); |
| 100 | + } |
| 101 | + |
| 102 | + } catch (IOException e) { |
| 103 | + e.printStackTrace(); |
| 104 | + } finally { |
| 105 | + try { |
| 106 | + response.close(); |
| 107 | + client.close(); |
| 108 | + } catch (IOException e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } |
| 111 | + } |
| 112 | + return content; |
| 113 | + } |
| 114 | +} |
0 commit comments