package com.ssjl.zhaobiao.guizhou.utils; import okhttp3.*; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.Map; @Component public class OkHttpUtil { private OkHttpClient client; private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); private static final MediaType WWWFORM = MediaType.get("application/x-www-form-urlencoded"); public OkHttpUtil() { client = new OkHttpClient(); } public String get(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } public String get(String url, Headers headers) throws IOException { Request request = new Request.Builder() .url(url) .headers(headers) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } public String postJson(String url, String json) throws IOException { RequestBody requestBody = RequestBody.create(json, JSON); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } public String postJson(String url, String json, Headers headers) throws IOException { RequestBody requestBody = RequestBody.create(json, JSON); Request request = new Request.Builder() .url(url) .post(requestBody) .headers(headers) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } public String postWwwForm(String url, Map formMap) throws IOException { FormBody.Builder formBuilder = new FormBody.Builder(); for(Map.Entry item : formMap.entrySet()){ formBuilder.add(item.getKey(), item.getValue()); } RequestBody requestBody = formBuilder.build(); Headers headers = new Headers.Builder().add("Content-Type", "application/x-www-form-urlencoded").build(); Request request = new Request.Builder() .url(url) .post(requestBody) .headers(headers) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } public String postFormdata(String url, Map formMap) throws IOException { FormBody.Builder formBuilder = new FormBody.Builder(); for(Map.Entry item : formMap.entrySet()){ formBuilder.add(item.getKey(), item.getValue()); } Headers headers = new Headers.Builder().add("Content-Type", "multipart/form-data").build(); RequestBody requestBody = formBuilder.build(); Request request=new Request.Builder() .url(url) .post(requestBody) .headers(headers) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } public byte[] postBytes(String url, byte[] bytes, Headers headers) throws IOException { MediaType mediaType = MediaType.parse("application/octet-stream;charset=UTF-8"); RequestBody requestBody = RequestBody.create(bytes, mediaType); Request request = new Request.Builder() .url(url) .post(requestBody) .headers(headers) .build(); try (Response response = client.newCall(request).execute()) { return response.body().bytes(); } } }