java调用接口(okhttp )

目前看到的有好几种

1、Java自带的java.io和java.net
2、Apache的HttpClient
上面2个比较老了,不准备用,就了解一下有这个东西就行了。参考来源:https://www.cnblogs.com/sinosoft/p/10556993.html
3、okhttp,这个是我自己瞎找的,看起来比较新
4、rest-assured,测试群里美团大佬推荐的。

一、okhttp 

github示例:https://github.com/square/okhttp

1、依赖

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.10.0</version>
        </dependency>
View Code

2、自己写的示例

常量

public class Constants {
    public static class Url {
        public static final String BASE_URL = "你的host";
        public static final String MAPPING_URL = BASE_URL + "你的path";
        public static final String SEARCH_URL = BASE_URL + "你的path";
    }

    public static class MediaType {
        public static final String JSON = "application/json; charset=utf-8";
        public static final String FORM = "application/x-www-from-urlencoded";
    }

}
View Code

封装的请求类

import constants.Constants;
import okhttp3.*;

import java.io.IOException;
import java.util.Objects;

public class HttpUtils {
    // 重载方式给params设置默认值
    public static String get(String url) throws IOException {
        return get(url, null);
    }

    public static String get(String url, String params) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request req;
        if (params == null) {
            req = new Request.Builder().url(url).get().build();
        } else {
            req = new Request.Builder().url(url + "?" + params).get().build();
        }
        Response rsp = client.newCall(req).execute();
        System.out.println("响应码:" + rsp.code());
        System.out.println("响应头:" + rsp.headers());
        return Objects.requireNonNull(rsp.body()).string();
    }

    public static String post(String url, String params) throws IOException {
        // 设置请求参数类型,准备好请求参数body
        MediaType type = MediaType.parse(Constants.MediaType.JSON);
        RequestBody body = RequestBody.create(params, type);

        // 创建client
        OkHttpClient client = new OkHttpClient();
        // 创建请求
        Request req = new Request.Builder().url(url).post(body).build();
        // 使用client 发送请求
        Response rsp = client.newCall(req).execute();
        System.out.println("响应码:" + rsp.code());
        System.out.println("响应头:" + rsp.headers());
        return Objects.requireNonNull(rsp.body()).string();
    }
}
View Code

测试类

import constants.Constants;
import myutil.HttpUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

public class TC3 {

    @Test
    public void testGet() throws IOException {
        String rsp = HttpUtils.get(Constants.Url.MAPPING_URL);
        System.out.println(rsp);
    }

    @Test(dataProvider = "test")
    public void testPost(String params) throws IOException {
        String rsp = HttpUtils.post(Constants.Url.SEARCH_URL, params);
        System.out.println(rsp);
    }

    @DataProvider(name = "test")
    public Object[] data() {
        return new Object[]{"{"query":{"bool":{"must":[{"match_all":{}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"aggs":{}}"};
    }
}
View Code

 

一个只会点点点的测试,有疑问可以在测试群(群号:330405140)问我
原文地址:https://www.cnblogs.com/yinwenbin/p/15352170.html