| | |
| | | |
| | | import android.util.Log; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | |
| | | import com.driver.igotrip.BuildConfig; |
| | | import com.driver.igotrip.R; |
| | | import com.driver.igotrip.base.Const; |
| | | import com.driver.igotrip.base.MyApplication; |
| | | import com.driver.igotrip.utils.LogUtils; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.lang.reflect.Type; |
| | | import java.security.KeyStore; |
| | | import java.security.cert.CertificateException; |
| | | import java.security.cert.CertificateFactory; |
| | | import java.security.cert.X509Certificate; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import javax.net.ssl.SSLSocketFactory; |
| | | import javax.net.ssl.TrustManager; |
| | | import javax.net.ssl.TrustManagerFactory; |
| | | import javax.net.ssl.X509TrustManager; |
| | | |
| | | import cn.sinata.xldutils.utils.SPUtils; |
| | | import io.reactivex.Observable; |
| | |
| | | */ |
| | | |
| | | public class HttpManager { |
| | | private static HttpManager httpManager; |
| | | private final ApiServics service = initBaseData().create(ApiServics.class); |
| | | //只信任指定证书的服务 安全级别高 |
| | | private final ApiServics service = initBaseData(true, false).create(ApiServics.class); |
| | | //信任所有证书的服务 安全级别低 |
| | | private final ApiServics allTrustService = initBaseData(false, true).create(ApiServics.class); |
| | | private Gson gson; |
| | | private Map<String, String> headerParams; |
| | | private HashMap<String, Object> map; |
| | | private final Map<String, String> headerParams = new HashMap<>(); |
| | | |
| | | private static final class HttpManagerHolder { |
| | | private static final HttpManager httpManager = new HttpManager(); |
| | | } |
| | | |
| | | public static synchronized HttpManager getInstance() { |
| | | if (httpManager == null) synchronized (HttpManager.class) { |
| | | if (httpManager == null) { |
| | | httpManager = new HttpManager(); |
| | | } |
| | | } |
| | | return httpManager; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化请求头,具体情况根据需求设置 |
| | | */ |
| | | public void initHeader() { |
| | | headerParams = new HashMap<>(); |
| | | map = new HashMap<>(); |
| | | // 传参方式为json |
| | | // headerParams.put("Content-Type", "application/json"); |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | } |
| | | |
| | | public HashMap<String, Object> getMap() { |
| | | if (map == null) { |
| | | map = new HashMap<>(); |
| | | } |
| | | map.clear(); |
| | | return map; |
| | | return HttpManagerHolder.httpManager; |
| | | } |
| | | |
| | | private static String bodyToString(final RequestBody request) { |
| | |
| | | } |
| | | } |
| | | |
| | | @Nullable |
| | | private SSLSocketFactory getSSLSocketFactory(X509TrustManager trustManager) { |
| | | try { |
| | | SSLContext tls = SSLContext.getInstance("TLS"); |
| | | tls.init(null, new TrustManager[]{trustManager}, null); |
| | | return tls.getSocketFactory(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 创建特定证书信任管理器 |
| | | */ |
| | | @Nullable |
| | | private X509TrustManager createTrustManager(int certificateResId) { |
| | | try { |
| | | CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
| | | try ( |
| | | // 读取证书文件 |
| | | InputStream inputStream = MyApplication.Companion.getAppContext().getResources().openRawResource(certificateResId) |
| | | ) { |
| | | X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream); |
| | | |
| | | // 创建包含该证书的KeyStore |
| | | KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| | | keyStore.load(null, null); |
| | | keyStore.setCertificateEntry("server", certificate); |
| | | |
| | | // 创建TrustManager |
| | | TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| | | trustManagerFactory.init(keyStore); |
| | | return (X509TrustManager) trustManagerFactory.getTrustManagers()[0]; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 创建所有证书信任管理器 |
| | | */ |
| | | @NonNull |
| | | private X509TrustManager createAllTrustManager() { |
| | | return new X509TrustManager() { |
| | | @Override |
| | | public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public X509Certificate[] getAcceptedIssuers() { |
| | | return new X509Certificate[0]; |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 初始化数据 |
| | | * |
| | | */ |
| | | private Retrofit initBaseData() { |
| | | initHeader(); |
| | | private Retrofit initBaseData(boolean isUseCert, boolean isTrustAll) { |
| | | // 监听请求条件 |
| | | OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
| | | builder.connectTimeout(3, TimeUnit.MINUTES); |
| | | builder.readTimeout(3, TimeUnit.MINUTES); |
| | | builder.writeTimeout(3, TimeUnit.MINUTES); |
| | | if (isUseCert) { |
| | | X509TrustManager trustManager = createTrustManager(R.raw.star_gopost_ci); |
| | | if (trustManager != null) { |
| | | SSLSocketFactory sslSocketFactory = getSSLSocketFactory(trustManager); |
| | | if (sslSocketFactory != null) { |
| | | builder.sslSocketFactory(sslSocketFactory, trustManager); |
| | | } |
| | | } |
| | | } else { |
| | | if (isTrustAll) { |
| | | X509TrustManager trustManager = createAllTrustManager(); |
| | | SSLSocketFactory sslSocketFactory = getSSLSocketFactory(trustManager); |
| | | if (sslSocketFactory != null) { |
| | | builder.sslSocketFactory(sslSocketFactory, trustManager); |
| | | } |
| | | } |
| | | } |
| | | // builder.addInterceptor(new Interceptor() { |
| | | // @Override |
| | | // public Response intercept(Chain chain) throws IOException { |
| | |
| | | } |
| | | |
| | | Log.i("zzz", "request====" + new JSONObject(params)); |
| | | |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | // getService.getResult(action.substring(action.lastIndexOf("/") + 1), headerParams, params) |
| | | service.getResult(action, headerParams, params) |
| | | .subscribeOn(Schedulers.io()) |
| | |
| | | // public <T extends BaseBean> void post(final String action, Map<String, Object> params, |
| | | public void post(final String action, Map<String, Object> params, |
| | | Observer<ResponseBody> observer) { |
| | | post(action, params) |
| | | post(false, action, params, observer); |
| | | } |
| | | |
| | | /** |
| | | * Post请求 |
| | | * |
| | | * @param isUseAllTrustService 是否使用信任所有证书服务 |
| | | * @param action 请求接口的尾址 |
| | | * @param params 请求参数 |
| | | */ |
| | | // public <T extends BaseBean> void post(final String action, Map<String, Object> params, |
| | | public void post(boolean isUseAllTrustService, final String action, Map<String, Object> params, |
| | | Observer<ResponseBody> observer) { |
| | | post(isUseAllTrustService, action, params) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(observer); |
| | |
| | | * @param params 请求参数 |
| | | */ |
| | | public Observable<ResponseBody> post(final String action, Map<String, Object> params) { |
| | | return post(false, action, params); |
| | | } |
| | | |
| | | /** |
| | | * Post请求 |
| | | * |
| | | * @param action 请求接口的尾址 |
| | | * @param params 请求参数 |
| | | */ |
| | | public Observable<ResponseBody> post(boolean isUseAllTrustService, final String action, Map<String, Object> params) { |
| | | if (params == null) { |
| | | params = new HashMap<>(); |
| | | } |
| | | int type = getTypeByLanguage(); |
| | | params.put(Const.language, type); // 1 中文 2 英文 3 法文 |
| | | LogUtils.d("post", params.toString()); |
| | | return service.postResult(action, headerParams, params); |
| | | headerParams.put("Authorization", "Bearer " + getToken()); |
| | | return (isUseAllTrustService ? allTrustService : service).postResult(action, headerParams, params); |
| | | } |
| | | |
| | | /** |