JAVA接口自动化框架5:完善post、get、put、delete等各种请求

本文档详细介绍了如何在JAVA接口自动化框架中编写和测试POST、GET、PUT、DELETE请求,包括配置properties文件、设置HOST、以及针对不同请求类型的测试用例编写,如Form表单提交和JSON格式的数据发送。
摘要由CSDN通过智能技术生成

本章主要介绍各种请求的编写,及测试类测试方法

一、各种请求的方法编写

package com.qa.restclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.qa.utils.fatjson.FastjsonUtils;

/*
 * 1.实现get请求方法及带请求头信息,POST请求方法,PUT方法,DELETE方法
 * 2.一个是带请求头信息的POST请求方法
 * 3.获取响应状态码
 * 4.json内容解析
 */
public class RestClient {
    
    private static final Logger log = LoggerFactory.getLogger(RestClient.class);
    
    // get请求方法
    public static CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
        return get(url, null);
    }

    // Get 请求方法(带请求头信息)
    public static CloseableHttpResponse get(String url, Map<String, String> headers)
            throws ClientProtocolException, IOException {
        // 创建一个可关闭的HttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建一个HttpGet的请求对象
        HttpGet httpget = new HttpGet(url);
        // 加载请求头到httpget对象
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpget.addHeader(entry.getKey(), entry.getValue());
            }
        }
        // 执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
        CloseableHttpResponse httpResponse = httpclient.execute(httpget);

        return httpResponse;
    }
    
    
    //POST方法(如果不需要header可传入null),提交form表单(默认application/x-www-form-urlencoded)
    public static CloseableHttpResponse postForm(String url, Map<String, String> params, Map<String, String> headers)
            throws ClientProtocolException, IOException {
        // 设置请求头数据传输格式,使用表单提交的方式,postman中有写
        //headers.put("Content-Type","application/x-www-form-urlencoded");
        return post(url, null, params, headers);
    }

    //  POST方法,发送json格式(如果不需要header可传入null)
    public static CloseableHttpResponse postJson(String url, String jsonString, Map<String, String> headers)
            throws ClientProtocolException, IOException {
        //准备请求头信息
        headers.put("Content-Type", "application/json");//postman中有写
        return post(url, jsonString, null, headers);
    }    
    
    
    // POST方法
    static CloseableHttpResponse post(String url, String jsonString, Map<String, String> params, Map<String, String> headers)
            throws ClientProtocolException, IOException {
        // 创建一个可关闭的HttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建一个HttpPost的请求对象
        HttpPost httppost = new HttpPost(url);
        // 构造请求体,创建参数队列
        if(jsonString != null && !"".equals(jsonString)) {
            httppost.setEntity(new St
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值