根据url获取html 方式其一

根据url获取html 方式其一

依赖

<!-- https://mvnrepository.com/artifact/com.liferay/org.apache.commons.httpclient -->
<dependency>
	<groupId>com.liferay</groupId>
	<artifactId>org.apache.commons.httpclient</artifactId>
	<version>3.1.LIFERAY-PATCHED-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.3.1</version>
</dependency>

代码

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class HttpClientUtil {


    public static HttpClient getClient() {
        HttpClient client = new HttpClient();
        return client;
    }

    public static String getHtml(String url) throws HttpException, IOException {
        return getHtml(url, 80, null, null, 0, null);
    }

    public static String getHtml(String url, String cookie) throws HttpException, IOException {
        return getHtml(url, 80, null, null, 0, cookie);
    }

    public static String getHtml(String url, int port, String cookie) throws HttpException, IOException {
        return getHtml(url, port, null, null, 0, cookie);
    }

    public static String getHtml(String url, int port, String encoding, String proxyHost, int proxyPort, String cookie)
            throws HttpException, IOException {
        HttpClient httpClient = getClient();
        String rest = null;
        if (proxyHost != null && proxyPort != 0) httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
        HttpMethod method = new GetMethod(url);
        if (!StringUtils.isBlank(cookie)) {
            method.addRequestHeader("Cookie", cookie);
        }
        method.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36");
        //Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
        httpClient.executeMethod(method);

        //根据http头解析正确的字符集
        String header = method.getResponseHeader("Content-Type").getValue();
        if (header.contains("charset=")) {
            encoding = header.substring(header.indexOf("charset=") + "charset=".length(), header.length());
        }
        if (encoding == null) encoding = "GBK";

        rest = new String(method.getResponseBody(), encoding);
        method.releaseConnection();
        return rest;
    }

    public static void main(String[] args) throws HttpException, IOException {
        String url = "https://www.baidu.com";
        System.out.println(getHtml(url));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值