|
@@ -0,0 +1,215 @@
|
|
|
+package com.ozs.common.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import okhttp3.Call;
|
|
|
+import okhttp3.Callback;
|
|
|
+import okhttp3.FormBody;
|
|
|
+import okhttp3.MediaType;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
+import okhttp3.Response;
|
|
|
+import okhttp3.ResponseBody;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class HttpClientUtil {
|
|
|
+ private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class);
|
|
|
+
|
|
|
+ private static OkHttpClient client = new OkHttpClient();
|
|
|
+
|
|
|
+ public static String post(String url, String params) throws Exception {
|
|
|
+ RequestBody body = new FormBody.Builder().add("params", params).build();
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ return response.body() != null ? response.body().string() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String post(String url, Map<String, String> params) throws Exception {
|
|
|
+ FormBody.Builder builder = new FormBody.Builder();
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
+ builder.add(entry.getKey(), entry.getValue());
|
|
|
+ log.info("params:" + entry.getKey() + " " + entry.getValue());
|
|
|
+ }
|
|
|
+ RequestBody body = builder.build();
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ return response.body() != null ? response.body().string() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String postBody(String url, String params) throws Exception {
|
|
|
+ RequestBody body = RequestBody.create(MediaType.parse("text/xml; charset=utf-8"), params);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ return response.body() != null ? response.body().string() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String postJson(String url, String json) throws IOException {
|
|
|
+ RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ return response.body() != null ? response.body().string() : null;
|
|
|
+ } else {
|
|
|
+ throw new IOException("Unexpected code " + response);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String putJson(String url, String json,String authorization) throws IOException {
|
|
|
+ RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .put(body)
|
|
|
+ .header("Authorization",authorization)
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ return response.body() != null ? response.body().string() : null;
|
|
|
+ } else {
|
|
|
+ throw new IOException("Unexpected code " + response);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String get(String url) throws Exception {
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .get()
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ return response.body() != null ? response.body().string() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static InputStream download(String url) throws Exception {
|
|
|
+ Request request = new Request.Builder().url(url).build();
|
|
|
+ ResponseBody body = client.newCall(request).execute().body();
|
|
|
+ return body != null ? body.byteStream() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getAndCheckSuccessful(String url) throws Exception {
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .get()
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new IOException("Unexpected code " + response);
|
|
|
+ }
|
|
|
+ return response.body() != null ? response.body().string() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void asyncPost(String url, Map<String, String> params) {
|
|
|
+ FormBody.Builder builder = new FormBody.Builder();
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
+ builder.add(entry.getKey(), entry.getValue());
|
|
|
+ log.info("params:" + entry.getKey() + " " + entry.getValue());
|
|
|
+ }
|
|
|
+ RequestBody body = builder.build();
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+ client.newCall(request).enqueue(new Callback() {
|
|
|
+ @Override
|
|
|
+ public void onFailure(Call call, IOException e) {
|
|
|
+ log.error("http post异步请求失败,异常原因:", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(Call call, Response response) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void asyncPost(String url, String json) {
|
|
|
+ RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+ client.newCall(request).enqueue(new Callback() {
|
|
|
+ @Override
|
|
|
+ public void onFailure(Call call, IOException e) {
|
|
|
+ log.error("http post异步请求失败,异常原因:", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(Call call, Response response) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static List<Districts> getP() throws Exception {
|
|
|
+ String url = "https://restapi.amap.com/v3/config/district?keywords=中华人民共和国&subdistrict=2&key=1038f05a58704762e009c8e726226c26";
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .get()
|
|
|
+ .build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ Map<String, Object> map = JSON.parseObject(response.body().string(), Map.class);
|
|
|
+ log.info("***************", map);
|
|
|
+ List<Districts> list = JSON.parseArray(JSON.toJSONString(map.get("districts")), Districts.class);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ getP();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class Districts {
|
|
|
+ String adcode;
|
|
|
+ String name;
|
|
|
+ List<Districts> districts;
|
|
|
+
|
|
|
+ public String getAdcode() {
|
|
|
+ return adcode;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setAdcode(String adcode) {
|
|
|
+ this.adcode = adcode;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getName() {
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setName(String name) {
|
|
|
+ this.name = name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Districts> getDistricts() {
|
|
|
+ return districts;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setDistricts(List<Districts> districts) {
|
|
|
+ this.districts = districts;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|