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();
    }
}

 

相关文章

可以认为OpenFeign是Feign的增强版,不同的是OpenFeign支持S...
为进一步规范小程序交易生态、提升用户购物体验、满足用户在...
云原生之使用Docker部署Dashdot服务器仪表盘
本文主要描述TensorFlow之回归模型的基本原理
1.漏洞描述Apache Druid 是一个集时间序列数据库、数据仓库和...
内部类(当作类中的一个普通成员变量,只不过此成员变量是cl...