通过url抓获得到的html

来源:互联网 发布:三十后换工作 知乎 编辑:程序博客网 时间:2024/06/05 08:30

需要的jar包
httpclient-4.5.jar
httpcore-4.4.1.jar

public static String findUrlHtml(String url, String encoding) {        String html = null;        CloseableHttpResponse response = null;        CloseableHttpClient httpclient = HttpClients.createDefault();        try {            HttpUriRequest request = new HttpGet(url);            response = httpclient.execute(request);            HttpEntity entity = response.getEntity();            if (entity != null) {                InputStream input = entity.getContent();                try {                    html = IOUtils.toString(input, encoding);                } finally {                    input.close();                }            }        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (response != null) {                    response.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return html;    }
原创粉丝点击