HttpClientUtil

 

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class HttpClientUtil {
    public static String get(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return convertStreamToString(entity.getContent());
                }
                return null;
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String post(String url, String paramJson) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost post = new HttpPost(url);
            HttpEntity ent = new StringEntity(paramJson, "utf8");
            post.setEntity(ent);
            CloseableHttpResponse response = httpclient.execute(post);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return convertStreamToString(entity.getContent());
                }
                return null;
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static String convertStreamToString(InputStream inputStream) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            System.out.println("Error=" + e.toString());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                System.out.println("Error=" + e.toString());
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String url = "https://xxxxxxxxxxxxx";

        String param = 
         "{\"msg\": \"hello world\"}"; String ss = post(url, param); System.err.println(ss); } }

 若是需要用现成的工具类,且设置header

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

//链式构建请求 String result2 = HttpRequest.post("www.baidu.com") .header(Header.USER_AGENT, String.valueOf(ContentType.FORM_URLENCODED))//头信息,多个头信息多次调用此方法即可 .form("paramMap")//表单内容 .timeout(20000)//超时,毫秒 .execute().body(); Console.log(result2);

 

posted @ 2021-12-09 10:23  君子笑而不语  阅读(46)  评论(0编辑  收藏  举报