| 
					
				 | 
			
			
				@@ -0,0 +1,145 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.ozs.common.utils.http; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import okhttp3.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.slf4j.LoggerFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.io.IOException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.io.InputStream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * HttpClient 工具 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @author JainWong 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+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 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) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |