zoukankan      html  css  js  c++  java
  • 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)问我
  • 相关阅读:
    weekly review 200908: Talk Show of ASP.NET
    weeklyreview 200911: Drowse of Spring
    数据库中标识种子(否,是,是(不用于复制))解释
    Hashtable.ContainsKey跟Hashtable.Contains的区别
    【Oracle学习起步1】用户解锁及密码输入问题
    C#弹出对话框实现
    因为文件组 'PRIMARY' 已满。
    SQL删除数据的各种方式总结
    C standard library contents
    scanf("%c",&c)前的printf函数调用问题
  • 原文地址:https://www.cnblogs.com/yinwenbin/p/15352170.html
Copyright © 2011-2022 走看看