java http HttpClientUtil 工具类

本文介绍了一个使用Java实现的HTTP客户端工具类,该工具类包括获取网页源码、POST请求及GET请求的方法。通过设置不同的参数,如超时时间和字符集,确保了网络请求的灵活性与稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class HttpClientUtil {
    private static final Log logger = LogFactory.getLog(UploaderController.class);


    private static final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.16) Gecko/20110319 BTRS96900 Firefox/3.6.16";
    public static String getSourceByUrl(final String url) {
        HttpClient httpClient = new HttpClient();
        String str = null;
        PostMethod getMethod = new PostMethod(url);
        getMethod.setRequestHeader("User-Agent",USER_AGENT);
        getMethod.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 0);
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(0);
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed: " + getMethod.getStatusLine());
            }
            str = IOUtils.toString(getMethod.getResponseBodyAsStream(), "UTF-8");
        } catch (UnknownHostException e) {
            logger.error("UnknownHostException: Please check your provided http address!");
        } catch (HttpException e) {
            logger.error("HttpException: Please check your provided http address!");
        } catch (ConnectException e) {
            logger.error("IOException: Please check your provided http address!");
        } catch (IOException e) {
            logger.error("IOException: Please check your provided http address!");
        } finally {
            getMethod.releaseConnection();
        }
        return str;
    }


    public static String post(String url, NameValuePair[] params) {
        HttpClient httpClient = new HttpClient();
        String str = null;
        PostMethod getMethod = new PostMethod(url);
        getMethod.setRequestHeader("User-Agent",USER_AGENT);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000);
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        if (params != null) {
            getMethod.addParameters(params);
        }
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed: " + getMethod.getStatusLine() + ",url:[" + url + "]");
            }
            str = getMethod.getResponseBodyAsString();
        } catch (Exception e) {
            logger.error("HttpClientUtil post error.url:[" + url + "]", e);
        } finally {
            getMethod.releaseConnection();
        }
        return str;
    }
    
    public static String get(String url) {
        HttpClient httpClient = new HttpClient();
        String str = null;
        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("User-Agent",USER_AGENT);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000);
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed: " + getMethod.getStatusLine() + ",url:[" + url + "]");
            }
            str = getMethod.getResponseBodyAsString();
        } catch (Exception e) {
            logger.error("HttpClientUtil get error.url:[" + url + "]", e);
        } finally {
            getMethod.releaseConnection();
        }
        return str;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值