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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM