|
@@ -0,0 +1,168 @@
|
|
|
|
+package com.flyer.foster.util;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import okhttp3.*;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+public class HttpUtils {
|
|
|
|
+ public static String getHeader(HttpServletRequest request, String key){
|
|
|
|
+ return request.getHeader(key);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 同步访问
|
|
|
|
+ * @param address
|
|
|
|
+ * @param paramsMap
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String httpPost(String address, Map<String,String> paramsMap) {
|
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
|
+ .connectTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .readTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .writeTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+// .sslSocketFactory(new SSLSocketClient().getSSLSocketFactory())//配置
|
|
|
|
+// .hostnameVerifier(new SSLSocketClient().getHostnameVerifier())//配置 //忽略验证证书
|
|
|
|
+ .build();
|
|
|
|
+ MediaType type = MediaType.parse("application/json;charset=utf-8");
|
|
|
|
+ RequestBody requestBody=RequestBody.create(type, JSON.toJSONString(paramsMap));
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
+ .url(address).post(requestBody)
|
|
|
|
+ .build();
|
|
|
|
+ try {
|
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
|
+ if(response.isSuccessful()){
|
|
|
|
+ return response.body().string();
|
|
|
|
+ }else{
|
|
|
|
+ System.out.println(address + "错误码:"+response.code());
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 同步访问
|
|
|
|
+ * @param address
|
|
|
|
+ * @param paramsMap
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static Response getResponse(String address, Map<String,String> paramsMap) {
|
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
|
+ .connectTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .readTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .writeTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+// .sslSocketFactory(new SSLSocketClient().getSSLSocketFactory())//配置
|
|
|
|
+// .hostnameVerifier(new SSLSocketClient().getHostnameVerifier())//配置 //忽略验证证书
|
|
|
|
+ .build();
|
|
|
|
+ MediaType type = MediaType.parse("application/json;charset=utf-8");
|
|
|
|
+ RequestBody requestBody=RequestBody.create(type, JSON.toJSONString(paramsMap));
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
+ .url(address).post(requestBody)
|
|
|
|
+ .build();
|
|
|
|
+ try {
|
|
|
|
+ return client.newCall(request).execute();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 同步访问
|
|
|
|
+ * @param address
|
|
|
|
+ * @param json
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String httpJsonPost(String address, String json) {
|
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
|
+ .connectTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .readTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .writeTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+// .sslSocketFactory(new SSLSocketClient().getSSLSocketFactory())//配置
|
|
|
|
+// .hostnameVerifier(new SSLSocketClient().getHostnameVerifier())//配置 //忽略验证证书
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
+ .url(address).post(body)
|
|
|
|
+ .build();
|
|
|
|
+ try {
|
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
|
+ if(response.isSuccessful()){
|
|
|
|
+ return response.body().string();
|
|
|
|
+ }else{
|
|
|
|
+ System.out.println(address + "错误码:"+response.code());
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 同步访问
|
|
|
|
+ * @param address
|
|
|
|
+ * @param json
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String httpJsonPost(String address, String json, Headers headers) {
|
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
|
+ .connectTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .readTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .writeTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+// .sslSocketFactory(new SSLSocketClient().getSSLSocketFactory())//配置
|
|
|
|
+// .hostnameVerifier(new SSLSocketClient().getHostnameVerifier())//配置 //忽略验证证书
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ RequestBody body = RequestBody.create(MediaType.parse("application/json;"), json);
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
+ .url(address).post(body).headers(headers)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
|
+ if(response.isSuccessful()){
|
|
|
|
+ return response.body().string();
|
|
|
|
+ }else{
|
|
|
|
+ System.out.println(address + "错误码:"+response.code());
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 同步访问
|
|
|
|
+ * @param address
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String httpGet(String address) {
|
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
|
+ .connectTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .readTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .writeTimeout(15, TimeUnit.SECONDS)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
+ .url(address).get()
|
|
|
|
+ .build();
|
|
|
|
+ try {
|
|
|
|
+ Response response= client.newCall(request).execute();
|
|
|
|
+ if(response.isSuccessful()){
|
|
|
|
+ return response.body().string();
|
|
|
|
+ }else{
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|