123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package com.nosum.common.util;
- import com.alibaba.fastjson.JSONObject;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 请求工具类
- */
- public class HttpClientUtils {
- /**
- * 发送get请求,利用java代码发送请求
- */
- public static String doGet(String url) {
- try {
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpGet httpGet = new HttpGet(url);
- // 发送了一个http请求
- CloseableHttpResponse response = httpclient.execute(httpGet);
- // 如果响应200成功,解析响应结果
- if (response.getStatusLine().getStatusCode() == 200) {
- // 获取响应的内容
- HttpEntity responseEntity = response.getEntity();
- return EntityUtils.toString(responseEntity);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 将字符串转换成map
- */
- public static Map<String, String> getMap(String responseEntity) {
- Map<String, String> map = new HashMap<>();
- // 以&来解析字符串
- String[] result = responseEntity.split("\\&");
- for (String str : result) {
- // 以=来解析字符串
- String[] split = str.split("=");
- // 将字符串存入map中
- if (split.length == 1) {
- map.put(split[0], null);
- } else {
- map.put(split[0], split[1]);
- }
- }
- return map;
- }
- /**
- * 通过json获得map
- */
- public static Map<String, String> getMapByJson(String responseEntity) {
- Map<String, String> map = new HashMap<>();
- JSONObject jsonObject = JSONObject.parseObject(responseEntity);
- for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
- String key = entry.getKey();
- // 将obj转换成string
- String value = String.valueOf(entry.getValue());
- map.put(key, value);
- }
- return map;
- }
- }
|