JAVA+Maven+TestNG+Jenkins搭建接口自动化框架(三)基于HttpClient的http请求封装

直接上代码

package utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtil {

    private static CloseableHttpClient client;
    private static RequestConfig config;

    //初始化
    static {
        config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(20000).build();
        client = HttpClients.custom().build();
    }

    public static String doGet(String url, Map<String,String> param, final Map<String,String> header){
        if (url.isEmpty()){
            return null;
        }
        try{
            //拼接url
            if (param.size() > 0){
                List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                for (Map.Entry<String,String > entry: param.entrySet()){
                    String value = entry.getValue();
                    if (!value.isEmpty()){
                        pairs.add(new BasicNameValuePair(entry.getKey(),value));
                    }
                }
                url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs),"UTF-8");
            }

            HttpGet httpGet = new HttpGet(url);

            //添加请求头
            if (header.size() > 0){
                header.forEach((key,value) -> httpGet.setHeader(key,value));
            }
            CloseableHttpResponse response = client.execute(httpGet);

            //判断请求是否成功
            int code = response.getStatusLine().getStatusCode();
            if (code != 200 ){
                httpGet.abort();
                throw new RuntimeException("HttpClient,error status code :" + code);
            }

            //获取返回参数
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }

            //释放请求,关闭连接
            EntityUtils.consume(entity);
            response.close();
            return result;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static String doPostByJson(String url, String json, final Map<String,String> header){
        if (url.isEmpty()){
            return null;
        }
        try{

            //设置请求体
            HttpPost httpPost = new HttpPost(url);
            StringEntity entity = new StringEntity(json, "utf-8");
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);

            //添加请求头
            if (header.size() > 0){
                header.forEach((key,value) -> httpPost.setHeader(key,value));
            }

            CloseableHttpResponse response = client.execute(httpPost);

            //判断请求是否成功
            int code = response.getStatusLine().getStatusCode();
            if (code != 200 ){
                httpPost.abort();
                throw new RuntimeException("HttpClient,error status code :" + code);
            }

            //获取返回参数
            HttpEntity responseEntity = response.getEntity();
            String result = null;
            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity, "UTF-8");
            }
            EntityUtils.consume(responseEntity);

            //释放请求,关闭连接
            response.close();
            return result;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String doPostByFORM(String url, Map<String,String> form, final Map<String,String> header){
        if (url.isEmpty()) {
            return null;
        }

        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("content-Type", "application/x-www-form-urlencoded");

            if (header.size() > 0) {
                header.forEach((key,value) -> httpPost.setHeader(key,value));
            }
            //form表单
            if (form.size() > 0) {
                ArrayList<BasicNameValuePair> list = new ArrayList<>();
                form.forEach((key, value) -> list.add(new BasicNameValuePair(String.valueOf(key), String.valueOf(value))));
                httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
            }

            response = client.execute(httpPost);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != 200) {
                httpPost.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }

            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
            }
            EntityUtils.consume(entity);
            response.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值