package com.tencent.smtt.utils; import android.os.Build; import com.baidu.tts.loopj.AsyncHttpClient; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; public class f { public interface a { void a(int i); } public static String a(String str, Map<String, String> map, byte[] bArr, a aVar, boolean z) { HttpURLConnection a2; if (bArr == null || (a2 = a(str, map)) == null) { return null; } if (z) { a(a2, bArr); } else { b(a2, bArr); } return a(a2, aVar, false); } public static String a(String str, byte[] bArr, a aVar, boolean z) { String str2; if (z) { try { str2 = h.a().c(); } catch (Exception e) { e.printStackTrace(); TbsLog.e("HttpUtils", "rsaKey exception #1: " + e); return null; } } else { str2 = g.a().b(); } String str3 = str + str2; if (z) { try { bArr = h.a().a(bArr); } catch (Exception e2) { e2.printStackTrace(); TbsLog.e("HttpUtils", "rsaKey exception #2: " + e2); } } else { bArr = g.a().a(bArr); } if (bArr == null) { TbsLog.e("HttpUtils", "postData is null"); return null; } HashMap hashMap = new HashMap(); hashMap.put(AsyncHttpClient.HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded"); hashMap.put("Content-Length", String.valueOf(bArr.length)); HttpURLConnection a2 = a(str3, hashMap); if (a2 == null) { return null; } b(a2, bArr); return a(a2, aVar, z); } private static String a(HttpURLConnection httpURLConnection, a aVar, boolean z) { ByteArrayOutputStream byteArrayOutputStream; Throwable th; Throwable th2; InputStream inputStream; InputStream inputStream2 = null; String str = null; inputStream2 = null; try { int responseCode = httpURLConnection.getResponseCode(); if (aVar != null) { aVar.a(responseCode); } if (responseCode == 200) { InputStream inputStream3 = httpURLConnection.getInputStream(); String contentEncoding = httpURLConnection.getContentEncoding(); inputStream = (contentEncoding == null || !contentEncoding.equalsIgnoreCase(AsyncHttpClient.ENCODING_GZIP)) ? (contentEncoding == null || !contentEncoding.equalsIgnoreCase("deflate")) ? inputStream3 : new InflaterInputStream(inputStream3, new Inflater(true)) : new GZIPInputStream(inputStream3); try { byteArrayOutputStream = new ByteArrayOutputStream(); } catch (Throwable th3) { inputStream2 = inputStream; th = th3; byteArrayOutputStream = null; a(inputStream2); a(byteArrayOutputStream); throw th; } try { byte[] bArr = new byte[128]; while (true) { int read = inputStream.read(bArr); if (read == -1) { break; } byteArrayOutputStream.write(bArr, 0, read); } str = z ? new String(byteArrayOutputStream.toByteArray(), "utf-8") : new String(g.a().c(byteArrayOutputStream.toByteArray())); } catch (Throwable th4) { inputStream2 = inputStream; th = th4; a(inputStream2); a(byteArrayOutputStream); throw th; } } else { inputStream = null; byteArrayOutputStream = null; } a(inputStream); a(byteArrayOutputStream); return str; } catch (Throwable th5) { th = th5; byteArrayOutputStream = null; a(inputStream2); a(byteArrayOutputStream); throw th; } } private static HttpURLConnection a(String str, Map<String, String> map) { Exception e; String str2; String str3; HttpURLConnection httpURLConnection = null; try { HttpURLConnection httpURLConnection2 = (HttpURLConnection) new URL(str).openConnection(); try { httpURLConnection2.setRequestMethod("POST"); httpURLConnection2.setDoOutput(true); httpURLConnection2.setDoInput(true); httpURLConnection2.setUseCaches(false); httpURLConnection2.setConnectTimeout(20000); if (Build.VERSION.SDK_INT > 13) { str3 = "Connection"; str2 = "close"; } else { str3 = "http.keepAlive"; str2 = "false"; } httpURLConnection2.setRequestProperty(str3, str2); for (Map.Entry<String, String> entry : map.entrySet()) { httpURLConnection2.setRequestProperty(entry.getKey(), entry.getValue()); } return httpURLConnection2; } catch (Exception e2) { e = e2; httpURLConnection = httpURLConnection2; TbsLog.e("HttpUtil", "initHttpPostURLConnection exception: " + e); return httpURLConnection; } } catch (Exception e3) { e = e3; TbsLog.e("HttpUtil", "initHttpPostURLConnection exception: " + e); return httpURLConnection; } } private static void a(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception unused) { } } } private static void a(HttpURLConnection httpURLConnection, byte[] bArr) { Throwable th; GZIPOutputStream gZIPOutputStream; Exception e; try { gZIPOutputStream = new GZIPOutputStream(new BufferedOutputStream(httpURLConnection.getOutputStream(), 204800)); try { gZIPOutputStream.write(bArr); gZIPOutputStream.flush(); } catch (Exception e2) { e = e2; } } catch (Exception e3) { e = e3; gZIPOutputStream = null; try { e.printStackTrace(); a(null); a(gZIPOutputStream); } catch (Throwable th2) { th = th2; a(null); a(gZIPOutputStream); throw th; } } catch (Throwable th3) { th = th3; gZIPOutputStream = null; a(null); a(gZIPOutputStream); throw th; } a(null); a(gZIPOutputStream); } private static void b(HttpURLConnection httpURLConnection, byte[] bArr) { OutputStream outputStream = null; try { outputStream = httpURLConnection.getOutputStream(); outputStream.write(bArr); outputStream.flush(); } catch (Exception e) { TbsLog.e("HttpUtil", "writePostData exception: " + e); } catch (Throwable th) { a(outputStream); throw th; } a(outputStream); } }