| | |
| | | import org.apache.http.message.BasicNameValuePair; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | |
| | | |
| | | /** |
| | | * httpClient 工具类 |
| | | * |
| | | * @create 2019-02-10 下午 2:49 |
| | | */ |
| | | @Component |
| | |
| | | |
| | | /** |
| | | * 静态内部类---作用:单例产生类的实例 |
| | | * @author Administrator |
| | | * |
| | | * @author Administrator |
| | | */ |
| | | private static class LazyHolder { |
| | | private static final HttpClientUtils INSTANCE = new HttpClientUtils(); |
| | | |
| | | } |
| | | HttpClientUtils(){} |
| | | public static HttpClientUtils getInstance(){ |
| | | |
| | | HttpClientUtils() { |
| | | } |
| | | |
| | | public static HttpClientUtils getInstance() { |
| | | return LazyHolder.INSTANCE; |
| | | } |
| | | |
| | | /** |
| | | * 发送 post请求 |
| | | * |
| | | * @param httpUrl 地址 |
| | | */ |
| | | public String sendHttpPost(String httpUrl) { |
| | |
| | | |
| | | /** |
| | | * 发送 post请求 |
| | | * |
| | | * @param httpUrl 地址 |
| | | * @param params 参数(格式:key1=value1&key2=value2) |
| | | * @param params 参数(格式:key1=value1&key2=value2) |
| | | */ |
| | | public String sendHttpPost(String httpUrl, String params) { |
| | | HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost |
| | |
| | | return sendHttpPost(httpPost); |
| | | } |
| | | |
| | | |
| | | public static String sendPost(String httpUrl, Map<String, Object> maps) { |
| | | |
| | | try { |
| | | // 1. 创建 URL 对象 |
| | | URL url = new URL(httpUrl); |
| | | // 2. 创建 HttpURLConnection 对象 |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | |
| | | // 3. 设置请求方法为 POST |
| | | connection.setRequestMethod("POST"); |
| | | |
| | | // 4. 设置 Content-Type 头部字段 |
| | | connection.setRequestProperty("Content-Type", "application/json"); |
| | | |
| | | // 5. 设置其他请求头部字段(可选) |
| | | // connection.setRequestProperty("Authorization", "Bearer token"); |
| | | // connection.setRequestProperty("User-Agent", "Mozilla/5.0"); |
| | | |
| | | // 6. 向服务器发送数据 |
| | | String requestBody = maps.toString(); |
| | | byte[] postData = requestBody.getBytes("UTF-8"); |
| | | connection.setDoOutput(true); |
| | | try (OutputStream outputStream = connection.getOutputStream()) { |
| | | outputStream.write(postData); |
| | | } |
| | | |
| | | // 7. 获取响应码 |
| | | int responseCode = connection.getResponseCode(); |
| | | |
| | | // 8. 获取响应数据 |
| | | try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| | | String line; |
| | | StringBuilder response = new StringBuilder(); |
| | | while ((line = reader.readLine()) != null) { |
| | | response.append(line); |
| | | } |
| | | System.out.println(response.toString()); |
| | | connection.disconnect(); |
| | | return response.toString(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 发送 post请求 |
| | | * |
| | | * @param httpUrl 地址 |
| | | * @param maps 参数 |
| | | * @param maps 参数 |
| | | */ |
| | | public String sendHttpPost(String httpUrl, Map<String, String> maps) { |
| | | HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost |
| | |
| | | return sendHttpPost(httpPost); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送Post请求 |
| | | * |
| | | * @param httpPost |
| | | * @return |
| | | */ |
| | |
| | | long execStart = System.currentTimeMillis(); |
| | | response = httpClient.execute(httpPost); |
| | | long execEnd = System.currentTimeMillis(); |
| | | System.out.println("=================执行post请求耗时:"+(execEnd-execStart)+"ms"); |
| | | System.out.println("=================执行post请求耗时:" + (execEnd - execStart) + "ms"); |
| | | long getStart = System.currentTimeMillis(); |
| | | entity = response.getEntity(); |
| | | responseContent = EntityUtils.toString(entity, "UTF-8"); |
| | | long getEnd = System.currentTimeMillis(); |
| | | System.out.println("=================获取响应结果耗时:"+(getEnd-getStart)+"ms"); |
| | | System.out.println("=================获取响应结果耗时:" + (getEnd - getStart) + "ms"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | |
| | | |
| | | /** |
| | | * 发送 get请求 |
| | | * |
| | | * @param httpUrl |
| | | */ |
| | | public String sendHttpGet(String httpUrl) { |
| | |
| | | |
| | | /** |
| | | * 发送 get请求Https |
| | | * |
| | | * @param httpUrl |
| | | */ |
| | | public String sendHttpsGet(String httpUrl) { |
| | |
| | | |
| | | /** |
| | | * 发送Get请求 |
| | | * |
| | | * @param httpGet |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 发送Get请求Https |
| | | * |
| | | * @param httpGet |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 发送xml数据 |
| | | * |
| | | * @param url |
| | | * @param xmlData |
| | | * @return |