翰林优商.apk(点击下载) / HttpClientUtil.java


package com.baidu.speech.utils.auth;

import com.baidu.speech.utils.LogUtil;
import com.czhj.sdk.common.Constants;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
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.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

public class HttpClientUtil {
    public static final String APPLICATION_JSON = "application/json";
    public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
    public static final int DEFAULT_TIMEOUT = 5000;
    private static final String REQUESTMETHOD_GET = "GET";
    private static final String REQUESTMETHOD_POST = "POST";
    private static final String TAG = "HttpClientUtil";
    private static SSLContext mSSLContext;
    public final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        /* class com.baidu.speech.utils.auth.HttpClientUtil.AnonymousClass1 */

        public boolean verify(String str, SSLSession sSLSession) {
            HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
            return defaultHostnameVerifier.verify("tts.baidu.com", sSLSession) || defaultHostnameVerifier.verify("tsn.baidu.com", sSLSession) || defaultHostnameVerifier.verify("upl.baidu.com", sSLSession) || defaultHostnameVerifier.verify("httpdns.baidubce.com", sSLSession) || defaultHostnameVerifier.verify("httpsdns.baidu.com", sSLSession) || HttpClientUtil.convertHostname(str).contains(".baidu.");
        }
    };
    private String contentType = null;
    private Map<String, String> headers = null;
    private HttpURLConnection httpURLConnection = null;
    private boolean isUrlEncodingEnabled = true;
    private int mTimeOut = 5000;
    private Proxy proxy = null;
    private int statusCode = -1;

    /* access modifiers changed from: private */
    public static String convertHostname(String str) {
        if (!"vse.baidu.com".equals(str) && !"vop.baidu.com".equals(str) && !"openapi.baidu.com".equals(str)) {
            "upl.baidu.com".equals(str);
        }
        return str;
    }

    private void makeRequest(String str, String str2, String str3, ResponseHandlerInterface responseHandlerInterface) {
        HttpURLConnection httpURLConnection2;
        try {
            URL url = !this.isUrlEncodingEnabled ? new URL(URLDecoder.decode(str, "UTF-8")) : new URL(str);
            if (url.getProtocol().toLowerCase().equals(Constants.HTTPS)) {
                Proxy proxy2 = this.proxy;
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) (proxy2 != null ? url.openConnection(proxy2) : url.openConnection());
                this.httpURLConnection = httpsURLConnection;
                HttpsURLConnection httpsURLConnection2 = httpsURLConnection;
                httpsURLConnection.setHostnameVerifier(this.DO_NOT_VERIFY);
            } else {
                Proxy proxy3 = this.proxy;
                this.httpURLConnection = (HttpURLConnection) (proxy3 != null ? url.openConnection(proxy3) : url.openConnection());
            }
            Map<String, String> map = this.headers;
            if (map != null) {
                for (String str4 : map.keySet()) {
                    this.httpURLConnection.setRequestProperty(str4, this.headers.get(str4));
                }
            }
            if (responseHandlerInterface instanceof RangeFileAsyncHttpResponseHandler) {
                LogUtil.d(TAG, "RangeFileAsyncHttpResponseHandler");
                ((RangeFileAsyncHttpResponseHandler) responseHandlerInterface).updateRequestHeaders(this.httpURLConnection);
            }
            responseHandlerInterface.setRequestURI(URI.create(str));
            this.httpURLConnection.setRequestMethod(str3);
            this.httpURLConnection.setConnectTimeout(this.mTimeOut);
            this.httpURLConnection.setReadTimeout(this.mTimeOut);
            if (str3 == "POST") {
                this.httpURLConnection.setDoInput(true);
                this.httpURLConnection.setDoOutput(true);
                OutputStream outputStream = this.httpURLConnection.getOutputStream();
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                bufferedOutputStream.write(str2.getBytes());
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
                outputStream.close();
            } else {
                this.httpURLConnection.setDoInput(true);
            }
            this.statusCode = this.httpURLConnection.getResponseCode();
            LogUtil.d(TAG, "ResponseCode: " + this.statusCode);
            this.contentType = this.httpURLConnection.getContentType();
            Map<String, List<String>> headerFields = this.httpURLConnection.getHeaderFields();
            HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
            httpResponseEntity.setContentLength((long) this.httpURLConnection.getContentLength());
            httpResponseEntity.setInputStream(this.httpURLConnection.getInputStream());
            httpResponseEntity.setHeader(headerFields);
            httpResponseEntity.setContentEncoding(this.httpURLConnection.getContentEncoding());
            httpResponseEntity.setContentType(this.contentType);
            httpResponseEntity.setResponseCode(this.statusCode);
            responseHandlerInterface.sendResponseMessage(httpResponseEntity);
            httpURLConnection2 = this.httpURLConnection;
            if (httpURLConnection2 == null) {
                return;
            }
        } catch (MalformedURLException e) {
            if (responseHandlerInterface != null) {
                responseHandlerInterface.sendErrorMessage(this.statusCode, null, this.contentType, e);
            }
            e.printStackTrace();
            httpURLConnection2 = this.httpURLConnection;
            if (httpURLConnection2 == null) {
                return;
            }
        } catch (SocketTimeoutException e2) {
            if (responseHandlerInterface != null) {
                responseHandlerInterface.sendErrorMessage(this.statusCode, null, this.contentType, e2);
            }
            e2.printStackTrace();
            httpURLConnection2 = this.httpURLConnection;
            if (httpURLConnection2 == null) {
                return;
            }
        } catch (InterruptedIOException e3) {
            LogUtil.d(TAG, e3.toString());
            httpURLConnection2 = this.httpURLConnection;
            if (httpURLConnection2 == null) {
                return;
            }
        } catch (IOException e4) {
            if (responseHandlerInterface != null) {
                responseHandlerInterface.sendErrorMessage(this.statusCode, null, this.contentType, e4);
            }
            e4.printStackTrace();
            httpURLConnection2 = this.httpURLConnection;
            if (httpURLConnection2 == null) {
                return;
            }
        } catch (Exception e5) {
            if (responseHandlerInterface != null) {
                responseHandlerInterface.sendErrorMessage(this.statusCode, null, this.contentType, e5);
            }
            httpURLConnection2 = this.httpURLConnection;
            if (httpURLConnection2 == null) {
                return;
            }
        } catch (Throwable th) {
            HttpURLConnection httpURLConnection3 = this.httpURLConnection;
            if (httpURLConnection3 != null) {
                httpURLConnection3.disconnect();
            }
            throw th;
        }
        httpURLConnection2.disconnect();
    }

    public void get(String str, ResponseHandlerInterface responseHandlerInterface) {
        makeRequest(str, "", "GET", responseHandlerInterface);
    }

    public int getTimeOut(int i) {
        return this.mTimeOut;
    }

    public boolean isUrlEncodingEnabled() {
        return this.isUrlEncodingEnabled;
    }

    public void post(String str, String str2, ResponseHandlerInterface responseHandlerInterface) {
        makeRequest(str, str2, "POST", responseHandlerInterface);
    }

    public void setHeaders(Map<String, String> map) {
        this.headers = map;
    }

    public void setProxy(String str, int i) {
        this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(str, i));
    }

    public void setTimeOut(int i) {
        if (i < 1000) {
            i = 5000;
        }
        this.mTimeOut = i;
    }

    public void setURLEncodingEnabled(boolean z) {
        this.isUrlEncodingEnabled = z;
    }

    public void stop() {
        HttpURLConnection httpURLConnection2 = this.httpURLConnection;
        if (httpURLConnection2 != null) {
            httpURLConnection2.disconnect();
        }
    }
}