(String url, Map<String, String> headers, Map<String, String> params)
| 11 | |
| 12 | // POST请求,表单参数 |
| 13 | public static String postForm(String url, Map<String, String> headers, Map<String, String> params) throws IOException { |
| 14 | FormBody.Builder formBuilder = new FormBody.Builder(); |
| 15 | if (params != null && !params.isEmpty()) { |
| 16 | for (Map.Entry<String, String> entry : params.entrySet()) { |
| 17 | formBuilder.add(entry.getKey(), entry.getValue()); |
| 18 | } |
| 19 | } |
| 20 | RequestBody body = formBuilder.build(); |
| 21 | |
| 22 | Request.Builder builder = new Request.Builder() |
| 23 | .url(url) |
| 24 | .post(body); |
| 25 | |
| 26 | if (headers != null && !headers.isEmpty()) { |
| 27 | for (Map.Entry<String, String> entry : headers.entrySet()) { |
| 28 | builder.addHeader(entry.getKey(), entry.getValue()); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | Request request = builder.build(); |
| 33 | try (Response response = client.newCall(request).execute()) { |
| 34 | if (!response.isSuccessful()) { |
| 35 | throw new IOException("Unexpected code " + response); |
| 36 | } |
| 37 | assert response.body() != null; |
| 38 | return response.body().string(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public static String postForm(String url, Map<String, String> headers, String data) throws IOException { |
| 43 |
no test coverage detected