接口测试Java--封装get/post方法

封装get/post方法

public class RestfulClient {
    private CloseableHttpClient httpclient;
    private HttpGet httpGet;
    private HttpPost httpPost;
    private CloseableHttpResponse httpResponse;
    private int responseCode;
    private JSONObject responseBody;
    private HashMap<String, String> responseHeads;

    //通过httpclient获取get请求的反馈
    public void getResponse(String url) throws ClientProtocolException, IOException{
        httpclient = HttpClients.createDefault();
        httpGet = new HttpGet(url);
        httpResponse = httpclient.execute(httpGet);
    }
    //通过httpclient获取post请求的反馈  带有请求头
    public void sendPost(String url, List<NameValuePair> params, HashMap<String, String> headers) throws ClientProtocolException, IOException{
        //创建post请求对象
        httpclient = HttpClients.createDefault();
        httpPost = new HttpPost(url);
        //设置请求主体格式
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        //设置头部信息
        Set<String> set = headers.keySet();
        for(Iterator<String> iterator = set.iterator(); iterator.hasNext();){
            String key = iterator.next();
            String value = headers.get(key);
            httpPost.addHeader(key, value);
        }
        httpResponse = httpclient.execute(httpPost);
    }
    //请求头为默认值的post请求  不带请求头
    public void sendPost(String url, List<NameValuePair> params) throws ClientProtocolException, IOException{
        //创建post请求对象
        httpclient = HttpClients.createDefault();
        httpPost = new HttpPost(url);
        //设置请求主体格式
        HashMap<String, String> headers = new HashMap<String, String>();//请求头设置默认
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        //设置头部信息
        Set<String> set = headers.keySet();
        for(Iterator<String> iterator = set.iterator(); iterator.hasNext();){
            String key = iterator.next();
            String value = headers.get(key);
            httpPost.addHeader(key, value);
        }
        httpResponse = httpclient.execute(httpPost);
    }
    public void sendPost(String url) throws ClientProtocolException, IOException{
        //创建post请求对象
        httpclient = HttpClients.createDefault();
        httpPost = new HttpPost(url);
        //设置请求主体格式

        HashMap<String, String> headers = new HashMap<String, String>();//请求头设置默认
        List<NameValuePair> params =new ArrayList<NameValuePair>();
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        //设置头部信息
        Set<String> set = headers.keySet();
        for(Iterator<String> iterator = set.iterator(); iterator.hasNext();){
            String key = iterator.next();
            String value = headers.get(key);
            httpPost.addHeader(key, value);
        }
        httpResponse = httpclient.execute(httpPost);
    }

    //以JSON格式获取到反馈的主体
    public JSONObject getBodyInJSON() throws ParseException, IOException{
        HttpEntity entity;
        String entityToString;
        entity = httpResponse.getEntity();
        entityToString = EntityUtils.toString(entity);
        responseBody = JSON.parseObject(entityToString);
        System.out.println("This is your response body  " + responseBody);
        return responseBody;
    }
    //以哈希图的方式获取到反馈头部
    public HashMap<String, String> getHeaderInHash(){
        Header[] headers;
        headers = httpResponse.getAllHeaders();
        responseHeads = new HashMap<String, String>();
        for(Header header:headers){
            responseHeads.put(header.getName(), header.getValue());
        }
        System.out.println("This is your response header  " + responseHeads);

        return    responseHeads;
    }
    //获取反馈状态码
    public int getCodeInNumber(){
        responseCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println("This is your response code  " + responseCode);
        return responseCode;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值