微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐