1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import okhttp3.*; public class TestHttpClient { @Test public void test(){ OkHttpClient client = new OkHttpClient().newBuilder() .connectTimeout(Duration.ofSeconds(100)) .readTimeout(Duration.ofSeconds(100)) .writeTimeout(Duration.ofSeconds(100)) .build(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "code=1"); Request request = new Request.Builder() .url("https://127.0.0.1:1111/post") .method("POST", body) .addHeader("Content-Type", "application/x-www-form-urlencoded") .build(); try { Response response = client.newCall(request).execute(); ResponseBody responseBody = response.body(); if (response.code() != 200 || responseBody == null) { System.out.println("nothing " + startNum); return; } String string = responseBody.string(); } catch (IOException e) { } } }
|
本文地址: https://github.com/maxzhao-it/blog/post/dd1cdfee/