zoukankan      html  css  js  c++  java
  • 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.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) {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpPost post = new HttpPost(url);
                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();
        }
    }
  • 相关阅读:
    【Java学习笔记】<集合框架>Iterator的子接口ListIterator
    【Java学习笔记】<集合框架>List特有的取出方式之一
    【Java学习笔记】集合框架Ⅱ
    【Java学习笔记】集合框架Ⅰ
    【PS】Ⅱ图像合成与渐变工具笔记
    【PS】Ⅰ基础及选框工具笔记
    [PS]简单的智能电视制作案例
    Spring中线程池的使用
    SpringBoot 多线程
    solr DIH 设置定时索引
  • 原文地址:https://www.cnblogs.com/xiaoliu66007/p/15665836.html
Copyright © 2011-2022 走看看