智能工厂.apk(点击下载) / HttpUtils.java


package cn.jiguang.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.text.TextUtils;
import cn.jiguang.aq.c;
import cn.jiguang.f.h;
import com.baidu.speech.utils.AsrError;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.Iterator;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.TrustManager;

public class HttpUtils {
    public static final String a = cn.jiguang.v.a.b(new byte[]{32, 0, 14, 41, 38, 38, 63, 1, 23, 43});

    public static abstract class HttpListener {
        /* access modifiers changed from: protected */
        public void a() {
        }

        /* access modifiers changed from: protected */
        public void a(HttpResponse httpResponse) {
        }
    }

    private static class a extends AsyncTask<HttpRequest, Void, HttpResponse> {
        private HttpListener a;
        private Context b;

        public a(Context context, HttpListener httpListener) {
            this.a = httpListener;
            this.b = context;
        }

        /* access modifiers changed from: protected */
        /* renamed from: a */
        public HttpResponse doInBackground(HttpRequest... httpRequestArr) {
            if (httpRequestArr == null || httpRequestArr.length == 0) {
                return null;
            }
            return HttpUtils.httpGet(this.b, httpRequestArr[0]);
        }

        /* access modifiers changed from: protected */
        /* renamed from: a */
        public void onPostExecute(HttpResponse httpResponse) {
            HttpListener httpListener = this.a;
            if (httpListener != null) {
                httpListener.a(httpResponse);
            }
        }

        /* access modifiers changed from: protected */
        public void onPreExecute() {
            HttpListener httpListener = this.a;
            if (httpListener != null) {
                httpListener.a();
            }
        }
    }

    private static class b extends AsyncTask<String, Void, HttpResponse> {
        private HttpListener a;
        private Context b;

        public b(HttpListener httpListener, Context context) {
            this.a = httpListener;
            this.b = context;
        }

        /* access modifiers changed from: protected */
        /* renamed from: a */
        public HttpResponse doInBackground(String... strArr) {
            if (strArr == null || strArr.length == 0) {
                return null;
            }
            return HttpUtils.httpGet(this.b, strArr[0]);
        }

        /* access modifiers changed from: protected */
        /* renamed from: a */
        public void onPostExecute(HttpResponse httpResponse) {
            HttpListener httpListener = this.a;
            if (httpListener != null) {
                httpListener.a(httpResponse);
            }
        }

        /* access modifiers changed from: protected */
        public void onPreExecute() {
            HttpListener httpListener = this.a;
            if (httpListener != null) {
                httpListener.a();
            }
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:106:0x0223, code lost:
        if (r9 == null) goto L_0x025c;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:110:0x0257, code lost:
        if (r9 != null) goto L_0x0259;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:111:0x0259, code lost:
        r9.disconnect();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:21:0x0092, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:22:0x0093, code lost:
        cn.jiguang.aq.c.h("HttpUtils", "set ssl config error:" + r2.getMessage());
     */
    /* JADX WARNING: Code restructure failed: missing block: B:60:0x0140, code lost:
        r8 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:61:0x0141, code lost:
        r9 = r7;
        r7 = null;
        r0 = r3;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:64:0x014c, code lost:
        r8 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:65:0x014d, code lost:
        r9 = r7;
        r7 = null;
        r0 = r3;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:66:0x0152, code lost:
        r8 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:67:0x0153, code lost:
        r9 = r7;
        r7 = null;
        r0 = r3;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:68:0x0158, code lost:
        r8 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:69:0x0159, code lost:
        r9 = r7;
        r7 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:70:0x015d, code lost:
        r8 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:71:0x015e, code lost:
        r9 = r7;
        r7 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:72:0x0161, code lost:
        r8 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:73:0x0162, code lost:
        r9 = r7;
        r7 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:74:0x0166, code lost:
        r8 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:75:0x0167, code lost:
        r9 = r7;
        r7 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:76:0x016b, code lost:
        r8 = th;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:77:0x016c, code lost:
        r9 = r7;
        r7 = null;
        r2 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:80:0x0176, code lost:
        r8 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:81:0x0177, code lost:
        r9 = r7;
        r7 = null;
        r2 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:82:0x017b, code lost:
        r8 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:83:0x017c, code lost:
        r9 = r7;
        r7 = null;
        r2 = null;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:92:0x01c7, code lost:
        if (r9 != null) goto L_0x0259;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Removed duplicated region for block: B:116:0x0269  */
    /* JADX WARNING: Removed duplicated region for block: B:42:0x00ff A[SYNTHETIC, Splitter:B:42:0x00ff] */
    /* JADX WARNING: Removed duplicated region for block: B:56:0x0123  */
    /* JADX WARNING: Removed duplicated region for block: B:59:0x013b  */
    /* JADX WARNING: Removed duplicated region for block: B:60:0x0140 A[ExcHandler: all (th java.lang.Throwable), Splitter:B:35:0x00f1] */
    /* JADX WARNING: Removed duplicated region for block: B:64:0x014c A[ExcHandler: IOException (e java.io.IOException), Splitter:B:35:0x00f1] */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x0152 A[ExcHandler: MalformedURLException (e java.net.MalformedURLException), Splitter:B:35:0x00f1] */
    /* JADX WARNING: Removed duplicated region for block: B:68:0x0158 A[ExcHandler: all (th java.lang.Throwable), PHI: r2 
      PHI: (r2v21 java.io.OutputStream) = (r2v22 java.io.OutputStream), (r2v22 java.io.OutputStream), (r2v24 java.io.OutputStream) binds: [B:31:0x00d0, B:32:?, B:27:0x00c4] A[DONT_GENERATE, DONT_INLINE], Splitter:B:27:0x00c4] */
    /* JADX WARNING: Removed duplicated region for block: B:72:0x0161 A[ExcHandler: IOException (e java.io.IOException), PHI: r2 
      PHI: (r2v20 java.io.OutputStream) = (r2v22 java.io.OutputStream), (r2v22 java.io.OutputStream), (r2v24 java.io.OutputStream) binds: [B:31:0x00d0, B:32:?, B:27:0x00c4] A[DONT_GENERATE, DONT_INLINE], Splitter:B:27:0x00c4] */
    /* JADX WARNING: Removed duplicated region for block: B:74:0x0166 A[ExcHandler: MalformedURLException (e java.net.MalformedURLException), PHI: r2 
      PHI: (r2v19 java.io.OutputStream) = (r2v22 java.io.OutputStream), (r2v22 java.io.OutputStream), (r2v24 java.io.OutputStream) binds: [B:31:0x00d0, B:32:?, B:27:0x00c4] A[DONT_GENERATE, DONT_INLINE], Splitter:B:27:0x00c4] */
    /* JADX WARNING: Removed duplicated region for block: B:76:0x016b A[ExcHandler: all (th java.lang.Throwable), Splitter:B:6:0x0015] */
    /* JADX WARNING: Removed duplicated region for block: B:80:0x0176 A[ExcHandler: IOException (e java.io.IOException), Splitter:B:6:0x0015] */
    /* JADX WARNING: Removed duplicated region for block: B:82:0x017b A[ExcHandler: MalformedURLException (e java.net.MalformedURLException), Splitter:B:6:0x0015] */
    /* JADX WARNING: Removed duplicated region for block: B:97:0x01dd  */
    /* JADX WARNING: Removed duplicated region for block: B:99:0x01e8  */
    private static HttpResponse a(Context context, HttpRequest httpRequest, boolean z) {
        HttpURLConnection httpURLConnection;
        Throwable th;
        Closeable closeable;
        OutputStream outputStream;
        String str;
        Throwable th2;
        int responseCode;
        InputStream inputStream;
        byte[] bArr;
        HttpsURLConnection httpsURLConnection;
        HostnameVerifier defaultHostVerifier;
        InputStream inputStream2 = null;
        if (httpRequest == null) {
            return null;
        }
        HttpResponse httpResponse = new HttpResponse(httpRequest.getUrl());
        try {
            HttpURLConnection httpURLConnectionWithProxy = getHttpURLConnectionWithProxy(context, httpRequest.getUrl());
            try {
                URL url = httpURLConnectionWithProxy.getURL();
                c.a("HttpUtils", "host:" + url.getHost() + ",port:" + url.getPort());
                a(httpRequest, httpURLConnectionWithProxy);
                if (httpURLConnectionWithProxy instanceof HttpsURLConnection) {
                    if (httpRequest.getSslTrustManager() != null) {
                        SSLContext instance = SSLContext.getInstance("TLS");
                        instance.init(null, new TrustManager[]{httpRequest.getSslTrustManager()}, new SecureRandom());
                        if (instance != null) {
                            ((HttpsURLConnection) httpURLConnectionWithProxy).setSSLSocketFactory(instance.getSocketFactory());
                        }
                    }
                    if (httpRequest.getHostnameVerifier() != null) {
                        httpsURLConnection = (HttpsURLConnection) httpURLConnectionWithProxy;
                        defaultHostVerifier = httpRequest.getHostnameVerifier();
                    } else {
                        httpsURLConnection = (HttpsURLConnection) httpURLConnectionWithProxy;
                        defaultHostVerifier = new DefaultHostVerifier(httpURLConnectionWithProxy.getURL().getHost());
                    }
                    httpsURLConnection.setHostnameVerifier(defaultHostVerifier);
                }
                if (z) {
                    httpURLConnectionWithProxy.setRequestMethod("POST");
                    httpURLConnectionWithProxy.setDoOutput(true);
                    httpURLConnectionWithProxy.setDoInput(true);
                    byte[] paras = httpRequest.getParas();
                    if (paras != null) {
                        outputStream = httpURLConnectionWithProxy.getOutputStream();
                        try {
                            outputStream.write(paras);
                            responseCode = httpURLConnectionWithProxy.getResponseCode();
                            httpResponse.setResponseCode(responseCode);
                            inputStream = httpURLConnectionWithProxy.getInputStream();
                        } catch (MalformedURLException e) {
                        } catch (IOException e2) {
                        } catch (Throwable th3) {
                        }
                        if (httpRequest.isHaveRspData() && inputStream != null) {
                            bArr = readInputStream(inputStream);
                            if (bArr != null) {
                                if (httpRequest.isRspDatazip()) {
                                    bArr = h.b(bArr);
                                }
                            }
                            if (bArr == null && responseCode != 200 && httpRequest.isNeedErrorInput()) {
                                inputStream2 = httpURLConnectionWithProxy.getErrorStream();
                                bArr = readInputStream(inputStream2);
                            }
                            if (bArr != null) {
                                httpResponse.setResponseBody(new String(bArr, "UTF-8"));
                            }
                            a(httpURLConnectionWithProxy, httpResponse);
                            h.a((Closeable) inputStream);
                            h.a((Closeable) inputStream2);
                            h.a(outputStream);
                            if (httpURLConnectionWithProxy != null) {
                                httpURLConnectionWithProxy.disconnect();
                            }
                            return httpResponse;
                        }
                        bArr = null;
                        inputStream2 = httpURLConnectionWithProxy.getErrorStream();
                        bArr = readInputStream(inputStream2);
                        if (bArr != null) {
                        }
                        a(httpURLConnectionWithProxy, httpResponse);
                        h.a((Closeable) inputStream);
                        h.a((Closeable) inputStream2);
                        h.a(outputStream);
                        if (httpURLConnectionWithProxy != null) {
                        }
                        return httpResponse;
                    }
                }
                outputStream = null;
                responseCode = httpURLConnectionWithProxy.getResponseCode();
                httpResponse.setResponseCode(responseCode);
                inputStream = httpURLConnectionWithProxy.getInputStream();
            } catch (MalformedURLException e3) {
            } catch (IOException e4) {
            } catch (Throwable th4) {
            }
            try {
                bArr = readInputStream(inputStream);
                if (bArr != null) {
                }
                inputStream2 = httpURLConnectionWithProxy.getErrorStream();
                bArr = readInputStream(inputStream2);
                if (bArr != null) {
                }
                a(httpURLConnectionWithProxy, httpResponse);
                h.a((Closeable) inputStream);
                h.a((Closeable) inputStream2);
                h.a(outputStream);
                if (httpURLConnectionWithProxy != null) {
                }
            } catch (MalformedURLException e5) {
            } catch (IOException e6) {
            } catch (Throwable th5) {
            }
        } catch (MalformedURLException e7) {
            MalformedURLException e8 = e7;
            Closeable closeable2 = null;
            httpURLConnection = null;
            outputStream = null;
            c.g("HttpUtils", "http post  error:" + e8.getMessage());
            httpResponse.setResponseCode(3004);
            httpResponse.setResponseBody("MalformedURLException");
            h.a((Closeable) inputStream2);
            h.a(closeable2);
            h.a(outputStream);
        } catch (IOException e9) {
            IOException e10 = e9;
            Closeable closeable3 = null;
            httpURLConnection = null;
            outputStream = null;
            httpResponse.setResponseCode(2998);
            httpResponse.setResponseBody("网络错误");
            if (!(e10 instanceof SocketTimeoutException)) {
                httpResponse.setResponseCode(AsrError.ERROR_AUDIO_RECORDER_OPEN);
                str = "请求超时";
            } else if (e10 instanceof UnknownHostException) {
                httpResponse.setResponseCode(AsrError.ERROR_AUDIO_RECORDER_NOT_AVAILABLE);
                str = "域名无效";
            } else {
                if (e10 instanceof SSLHandshakeException) {
                    httpResponse.setResponseCode(3005);
                    str = "SSL失败";
                }
                c.g("HttpUtils", "http post IOException error:" + e10.getMessage());
                h.a((Closeable) inputStream2);
                h.a(closeable3);
                h.a(outputStream);
            }
            httpResponse.setResponseBody(str);
            c.g("HttpUtils", "http post IOException error:" + e10.getMessage());
            h.a((Closeable) inputStream2);
            h.a(closeable3);
            h.a(outputStream);
        } catch (Throwable th6) {
            th = th6;
            closeable = null;
            httpURLConnection = null;
            outputStream = null;
            h.a((Closeable) inputStream2);
            h.a(closeable);
            h.a(outputStream);
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
            throw th;
        }
        return httpResponse;
    }

    private static void a(HttpRequest httpRequest, HttpURLConnection httpURLConnection) {
        if (httpRequest != null && httpURLConnection != null) {
            setURLConnection(httpRequest.getRequestProperties(), httpURLConnection);
            if (httpRequest.getConnectTimeout() >= 0) {
                httpURLConnection.setConnectTimeout(httpRequest.getConnectTimeout());
            }
            if (httpRequest.getReadTimeout() >= 0) {
                httpURLConnection.setReadTimeout(httpRequest.getReadTimeout());
            }
        }
    }

    private static void a(HttpURLConnection httpURLConnection, HttpResponse httpResponse) {
        if (httpResponse != null && httpURLConnection != null) {
            httpResponse.setResponseCode(httpURLConnection.getResponseCode());
            httpResponse.setResponseHeader("expires", httpURLConnection.getHeaderField("Expires"));
            httpResponse.setResponseHeader("cache-control", httpURLConnection.getHeaderField("Cache-Control"));
        }
    }

    public static String appendParaToUrl(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        sb.append(!str.contains("?") ? "?" : "&");
        sb.append(str2);
        sb.append("=");
        sb.append(str3);
        return sb.toString();
    }

    public static HttpURLConnection getHttpURLConnectionWithProxy(Context context, String str) {
        NetworkInfo activeNetworkInfo;
        String extraInfo;
        URL url = new URL(str);
        if (context != null) {
            try {
                if (!(context.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", context.getPackageName()) != 0 || (activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo()) == null || activeNetworkInfo.getType() == 1 || (extraInfo = activeNetworkInfo.getExtraInfo()) == null || (!extraInfo.equals("cmwap") && !extraInfo.equals("3gwap") && !extraInfo.equals("uniwap")))) {
                    return (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(a, 80)));
                }
            } catch (Throwable unused) {
            }
        }
        return (HttpURLConnection) url.openConnection();
    }

    public static String getUrlWithParas(String str, Map<String, String> map) {
        if (TextUtils.isEmpty(str)) {
            str = "";
        }
        StringBuilder sb = new StringBuilder(str);
        String joinParas = joinParas(map);
        if (!TextUtils.isEmpty(joinParas)) {
            sb.append("?");
            sb.append(joinParas);
        }
        return sb.toString();
    }

    public static String getUrlWithValueEncodeParas(String str, Map<String, String> map) {
        if (TextUtils.isEmpty(str)) {
            str = "";
        }
        StringBuilder sb = new StringBuilder(str);
        String joinParasWithEncodedValue = joinParasWithEncodedValue(map);
        if (!TextUtils.isEmpty(joinParasWithEncodedValue)) {
            sb.append("?");
            sb.append(joinParasWithEncodedValue);
        }
        return sb.toString();
    }

    public static HttpResponse httpGet(Context context, HttpRequest httpRequest) {
        return a(context, httpRequest, false);
    }

    public static HttpResponse httpGet(Context context, String str) {
        return httpGet(context, new HttpRequest(str));
    }

    public static void httpGet(Context context, HttpRequest httpRequest, HttpListener httpListener) {
        new a(context, httpListener).execute(httpRequest);
    }

    public static void httpGet(Context context, String str, HttpListener httpListener) {
        new b(httpListener, context).execute(str);
    }

    public static String httpGetString(Context context, HttpRequest httpRequest) {
        HttpResponse httpGet = httpGet(context, httpRequest);
        if (httpGet == null) {
            return null;
        }
        return httpGet.getResponseBody();
    }

    public static String httpGetString(Context context, String str) {
        HttpResponse httpGet = httpGet(context, new HttpRequest(str));
        if (httpGet == null) {
            return null;
        }
        return httpGet.getResponseBody();
    }

    public static HttpResponse httpPost(Context context, HttpRequest httpRequest) {
        return a(context, httpRequest, true);
    }

    public static HttpResponse httpPost(Context context, String str) {
        return httpPost(context, new HttpRequest(str));
    }

    public static String httpPostString(Context context, String str) {
        HttpResponse httpPost = httpPost(context, new HttpRequest(str));
        if (httpPost == null) {
            return null;
        }
        return httpPost.getResponseBody();
    }

    public static String httpPostString(Context context, String str, Map<String, String> map) {
        HttpResponse httpPost = httpPost(context, new HttpRequest(str, map));
        if (httpPost == null) {
            return null;
        }
        return httpPost.getResponseBody();
    }

    public static String joinParas(Map<String, String> map) {
        if (map == null || map.size() == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> next = it.next();
            sb.append(next.getKey());
            sb.append("=");
            sb.append(next.getValue());
            if (it.hasNext()) {
                sb.append("&");
            }
        }
        return sb.toString();
    }

    public static String joinParasWithEncodedValue(Map<String, String> map) {
        StringBuilder sb = new StringBuilder("");
        if (map != null && map.size() > 0) {
            Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                try {
                    Map.Entry<String, String> next = it.next();
                    sb.append(next.getKey());
                    sb.append("=");
                    sb.append(URLEncoder.encode(next.getValue(), "UTF-8"));
                    if (it.hasNext()) {
                        sb.append("&");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static long parseGmtTime(String str) {
        try {
            return cn.jiguang.f.b.a("EEE, d MMM yyyy HH:mm:ss z").parse(str).getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static byte[] readInputStream(InputStream inputStream) {
        return h.b(inputStream);
    }

    public static void setURLConnection(Map<String, String> map, HttpURLConnection httpURLConnection) {
        if (!(map == null || map.size() == 0 || httpURLConnection == null)) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (!TextUtils.isEmpty(entry.getKey())) {
                    httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
        }
    }
}