zoukankan      html  css  js  c++  java
  • commons-httpclient 实现get和post请求

    PS:这个jar包,在2007年之后就没有更新过了, 是比较老的版本了。追求新的版本 用HttpComponents 比较好

    引入的jar包为:

    <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>

    具体实现类为:

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.methods.RequestEntity;
    import org.apache.commons.httpclient.methods.StringRequestEntity;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.IOException;
    
    public class HttpClientHelper {
        private static Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);
    
        private HttpClientHelper() {
    
        }
    
        /**
         * 发起POST请求
         *
         * @param url       url
         * @param paramJson 参数的json格式
         */
        public static String sendPost(String url, String paramJson) {
            logger.info("开始发起POST请求,请求地址为{},参数为{}", url, paramJson);
    
            // 创建httpClient实例对象
            HttpClient httpClient = new HttpClient();
            // 设置httpClient连接主机服务器超时时间:15000毫秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
            // 创建post请求方法实例对象
            PostMethod postMethod = new PostMethod(url);
            // 设置post请求超时时间
            postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
            postMethod.addRequestHeader("Content-Type", "application/json");
            try {
                //json格式的参数解析
                RequestEntity entity = new StringRequestEntity(paramJson, "application/json", "UTF-8");
                postMethod.setRequestEntity(entity);
    
                httpClient.executeMethod(postMethod);
                String result = postMethod.getResponseBodyAsString();
                postMethod.releaseConnection();
                return result;
            } catch (IOException e) {
                logger.error("POST请求发出失败,请求的地址为{},参数为{},错误信息为{}", url, paramJson, e.getMessage(), e);
            }
            return null;
        }
    
        /**
         * 发起GET请求
         *
         * @param urlParam url请求,包含参数
         */
        public static String sendGet(String urlParam) {
            logger.info("开始发起GET请求,请求地址为{}", urlParam);
            // 创建httpClient实例对象
            HttpClient httpClient = new HttpClient();
            // 设置httpClient连接主机服务器超时时间:15000毫秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
            // 创建GET请求方法实例对象
            GetMethod getMethod = new GetMethod(urlParam);
            // 设置post请求超时时间
            getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
            getMethod.addRequestHeader("Content-Type", "application/json");
            try {
                httpClient.executeMethod(getMethod);
                String result = getMethod.getResponseBodyAsString();
                getMethod.releaseConnection();
                logger.info("返回信息为{}", result);
                return result;
            } catch (IOException e) {
                logger.error("GET请求发出失败,请求的地址为{},错误信息为{}", urlParam, e.getMessage(), e);
            }
            return null;
        }
    
        public static void main(String[] args) {
            String url = "https://jiashubing.cn/tencenttest";
            String param = "{"aaa":"bbbbbbb"}";
            sendPost(url, param);
            String urlParam = "https://jiashubing.cn/talk/document?fileid=1234";
            sendGet(urlParam);
        }
    
    }
  • 相关阅读:
    Using Resource File on DotNet
    C++/CLI VS CSharp
    JIT VS NGen
    [Tip: disable vc intellisense]VS2008 VC Intelisense issue
    UVa 10891 Game of Sum(经典博弈区间DP)
    UVa 10723 Cyborg Genes(LCS变种)
    UVa 607 Scheduling Lectures(简单DP)
    UVa 10401 Injured Queen Problem(简单DP)
    UVa 10313 Pay the Price(类似数字分解DP)
    UVa 10635 Prince and Princess(LCS N*logN)
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/commons_httpclient.html
Copyright © 2011-2022 走看看