http工具类

httpUtil 工具类

/**
 * Created by xucd on 2017/6/28.
 */
public class HttpUtil {
    /**
     * 构建httpPost对象
     * @param vo
     * @return
     */
    public static HttpPost createHttpPost(HttpVO vo){
        HttpPost httpPost = new HttpPost(vo.getUrl());
        // 设置超时
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(1500)
                .setConnectTimeout(1500)
                .setSocketTimeout(1500).build();
        httpPost.setConfig(requestConfig);
        httpPost.setEntity(vo.getEntity());
        return httpPost;
    }


    /**
     * http post请求
     * @param vo
     * @return
     * @throws Exception
     */
    public static HttpReturnBO post(HttpVO vo) throws Exception{
        HttpReturnBO httpReturnBO = new HttpReturnBO();
        CloseableHttpClient httpclient = null;
        HttpPost httpPost = null;
        CloseableHttpResponse response = null;
        try {
            httpclient = HttpClients.createDefault();
            httpPost = HttpUtil.createHttpPost(vo);
            response = httpclient.execute(httpPost);
            httpReturnBO = HttpUtil.createReturnBo(response);

        }catch(Exception e){
            httpReturnBO.setStatus(false);
            httpReturnBO.setMessage("请求发生异常,网络异常(50001)");

            e.printStackTrace();
            System.out.println("post请求地址异常,url:" + vo.getUrl());
        } finally {
            if(response!=null){
                response.close();
            }
            if (httpPost != null) {
                httpPost.abort();
            }
            if(httpclient!=null){
                httpclient.close();
            }
        }
        return httpReturnBO;
    }

    /**
     * 构建httpGet对象
     * @param vo
     * @return
     */
    public static HttpGet createHttpGet(HttpVO vo){
        HttpGet httpGet = new HttpGet(vo.getUrl());
        // 设置超时
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(1500)
                .setConnectTimeout(1500)
                .setSocketTimeout(1500).build();
        httpGet.setConfig(requestConfig);

        return httpGet;
    }

    /**
     * http get请求
     * @param vo
     * @return
     * @throws Exception
     */
    public static HttpReturnBO get(HttpVO vo) throws Exception{
        HttpReturnBO httpReturnBO = new HttpReturnBO();
        CloseableHttpClient httpclient = null;
        HttpGet httpGet = null;
        CloseableHttpResponse response = null;
        try {
            httpclient = HttpClients.createDefault();
            httpGet = HttpUtil.createHttpGet(vo);
            response = httpclient.execute(httpGet);
            httpReturnBO = HttpUtil.createReturnBo(response);
        }catch(Exception e){
            httpReturnBO.setStatus(false);
            httpReturnBO.setMessage("请求发生异常,网络异常(50001)");

            e.printStackTrace();
            System.out.println("get请求地址异常,url:" + vo.getUrl());
        } finally {
            if(response!=null){
                response.close();
            }
            if (httpGet != null) {
                httpGet.abort();
            }
            if(httpclient!=null){
                httpclient.close();
            }
        }
        return httpReturnBO;
    }

    /**
     * 创建http请求返回参数对象
     * @param response
     * @return
     * @throws Exception
     */
    public static HttpReturnBO createReturnBo(CloseableHttpResponse response) throws Exception{
        HttpReturnBO httpReturnBO = new HttpReturnBO();
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            httpReturnBO.setStatus(true);
            String rev = EntityUtils.toString(response.getEntity());
            httpReturnBO.setMessage(rev);
        }else{
            httpReturnBO.setStatus(false);
            httpReturnBO.setMessage("请求失败,网络异常("+code+")");
        }
        return httpReturnBO;
    }
}

HttpReturnBO 业务类

/**
 * Created by xucd on 2017/6/28.
 */
public class HttpReturnBO {
    // 1:请求正常;2:请求异常
    private boolean status = false;
    private String message = "网络异常(50001)";

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

HttpVO 类

/**
 * Created by xucd on 2017/6/28.
 */
public class HttpVO implements HttpInterfaceVO{
    // 请求地址
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public UrlEncodedFormEntity getEntity() {
        return null;
    }
}

HttpInterfaceVO 接口

/**
 * Created by xucd on 2017/6/28.
 */
public interface HttpInterfaceVO {
    /**
     * 获取http要带的参数
     * @return
     */
    UrlEncodedFormEntity getEntity();
}

环境配置需要

pom.xml依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.6</version>
</dependency>

 

 

 

转载于:https://my.oschina.net/u/2985161/blog/1068659

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值