使用httpClient发送get\post请求

方式一:HttpClientUtil工具类:

public class httpClientUtil {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    /**
     * get请求,参数拼接在地址上
     * @param url 请求地址加参数
     * @return 响应
     */
    public String get(String url)
    {
        String result = null;

        HttpGet get = new HttpGet(url);

        try {
            response = httpClient.execute(get);
            if(response != null && response.getStatusLine().getStatusCode() == 200)
            {
                HttpEntity entity = response.getEntity();
                result = entityToString(entity);
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           closed();
        }
        return null;
    }
    /**
     * get请求,参数放在map里
     * @param url 请求地址
     * @param map 参数map
     * @return 响应
     */
    public String getMap(String url,Map<String,String> map)
    {
        String result = null;
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        for(Map.Entry<String,String> entry : map.entrySet())
        {
            pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
        }
        try {
            URIBuilder builder = new URIBuilder(url);
            builder.setParameters(pairs);
            HttpGet get = new HttpGet(builder.build());
            response = httpClient.execute(get);
            if(response != null && response.getStatusLine().getStatusCode() == 200)
            {
                HttpEntity entity = response.getEntity();
                result = entityToString(entity);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
           closed();
        }

        return null;
    }
    /**
     * 发送post请求,参数用map接收
     * @param url 地址
     * @param map 参数
     * @return 返回值
     */
    public String postMap(String url,Map<String,String> map) {
        String result = null;
        HttpPost post = new HttpPost(url);
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        for(Map.Entry<String,String> entry : map.entrySet())
        {
            pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
        }
        try {
            post.setEntity(new UrlEncodedFormEntity(pairs,"UTF-8"));
            response = httpClient.execute(post);
            if(response != null && response.getStatusLine().getStatusCode() == 200)
            {
                HttpEntity entity = response.getEntity();
                result = entityToString(entity);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
           closed();
        }
        return null;
    }
    /**
     * post请求,参数为json字符串
     * @param url 请求地址
     * @param jsonString json字符串
     * @return 响应
     */
    public String postJson(String url,String jsonString)
    {
        String result = null;
        HttpPost post = new HttpPost(url);
        try {
            post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
            response = httpClient.execute(post);
            if(response != null && response.getStatusLine().getStatusCode() == 200)
            {
                HttpEntity entity = response.getEntity();
                result = entityToString(entity);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closed();
        }
        return null;
    }
    private String entityToString(HttpEntity entity) throws IOException {
        String result = null;
        if(entity != null)
        {
            long lenth = entity.getContentLength();
            if(lenth != -1 && lenth < 2048)
            {
                result = EntityUtils.toString(entity,"UTF-8");
            }else {
                InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
                CharArrayBuffer buffer = new CharArrayBuffer(2048);
                char[] tmp = new char[1024];
                int l;
                while((l = reader1.read(tmp)) != -1) {
                    buffer.append(tmp, 0, l);
                }
                result = buffer.toString();
            }
        }
        return result;
    }
    public void closed(){
        try {
            httpClient.close();
            if(response != null)
            {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

测试controller:

@RestController
@RequestMapping("htt")
public class HttpClientController {

    @RequestMapping("httpclient")
    public String test(httpClientUtil httpClientUtil){
        String str = httpClientUtil.get("http://www.baidu.com");
        System.out.println(str);
        return "hello";
    }
}

方式二:HttpClientUtil工具类:

public class httpClientUtil {
    public String client(String url, HttpMethod method, MultiValueMap<String,String> map){
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url,String.class);
        return responseEntity.getBody();

    }

测试controller:

@RestController
@RequestMapping("htt")
public class HttpClientController {

    @RequestMapping("httpclient")
    public String test(httpClientUtil httpClientUtil){
        String url = "http://www.baidu.com";
        HttpMethod method = HttpMethod.GET;
        MultiValueMap map = new LinkedMultiValueMap();
        String str = httpClientUtil.client(url,method,map);
        System.out.println(str);
        return "hello";
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39654286/article/details/85003424