package sp3; import com.tencent.mm.plugin.report.service.q; import com.tencent.mm.sdk.platformtools.Log; import com.tencent.mm.sdk.platformtools.Util; import com.tencent.xweb.WebResourceResponse; import com.tencent.youtu.sdkkitframework.common.StateEvent; import ek4.c; import ek4.d0; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import lh4.m; import org.json.JSONObject; public final class a { /* renamed from: a reason: collision with root package name */ public static final a f304407a = new a(); /* JADX WARNING: Removed duplicated region for block: B:50:0x0176 A[SYNTHETIC, Splitter:B:50:0x0176] */ /* JADX WARNING: Removed duplicated region for block: B:57:0x0196 A[SYNTHETIC, Splitter:B:57:0x0196] */ /* JADX WARNING: Removed duplicated region for block: B:66:0x01a5 A[Catch:{ all -> 0x019f }] */ /* JADX WARNING: Removed duplicated region for block: B:89:0x0278 A[Catch:{ all -> 0x019f }] */ public final WebResourceResponse a(String str) { boolean z15; String str2; String str3; String str4; boolean z16; String str5; boolean z17; StringBuilder sb5; Object th5; HttpURLConnection httpURLConnection; Object th6; String str6; String str7; String str8; long currentTimeMillis = System.currentTimeMillis(); q.INSTANCE.t(1951, 3); if (str == null || str.length() == 0) { z15 = true; } else { z15 = false; } if (z15) { return null; } try { str2 = URLEncoder.encode(Util.nullAsNil(str), "UTF-8"); m.f(str2, "encode(Util.nullAsNil(value), \"UTF-8\")"); } catch (Exception e15) { Log.e("AdH5ContentFetcher", "appendUrlParamsWithUrlEncode exp:" + e15 + ", " + str); str2 = ""; } try { int H = d0.H("https://review.ugdtimg.com/page/api/get_html", "#", 0, false, 6, null); if (H >= 0) { str6 = "https://review.ugdtimg.com/page/api/get_html".substring(0, H); m.f(str6, "this as java.lang.String…ing(startIndex, endIndex)"); str7 = "https://review.ugdtimg.com/page/api/get_html".substring(H); m.f(str7, "this as java.lang.String).substring(startIndex)"); } else { str6 = "https://review.ugdtimg.com/page/api/get_html"; str7 = ""; } if (d0.z(str6, "?", false, 2, null)) { str8 = str6 + '&' + "url" + '=' + str2; } else { str8 = str6 + '?' + "url" + '=' + str2; } str3 = str8 + str7; } catch (Exception e16) { Log.e("AdH5ContentFetcher", "appendUrlParams exp:" + e16); str3 = "https://review.ugdtimg.com/page/api/get_html"; } m.g(str3, "url"); try { httpURLConnection = b(str3); if (httpURLConnection != null) { try { int responseCode = httpURLConnection.getResponseCode(); Log.i("AdH5ContentFetcher", "fetchUrlContent, retCode=" + responseCode); if (responseCode == 200) { str4 = d(new BufferedInputStream(httpURLConnection.getInputStream())); try { httpURLConnection.disconnect(); } catch (Throwable th7) { Log.e("AdH5ContentFetcher", "fetchUrlContent, disconnect exp=" + th7 + ", url=" + str3); } if (str4 != null) { try { if (str4.length() != 0) { z16 = false; if (z16) { q qVar = q.INSTANCE; qVar.t(1951, 4); JSONObject jSONObject = new JSONObject(str4); int optInt = jSONObject.optInt("code", -1); String optString = jSONObject.optString(StateEvent.Name.MESSAGE); String optString2 = jSONObject.optString("data"); long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis; Log.i("AdH5ContentFetcher", "fetchUrlResponse, [code=" + optInt + ", msg=" + optString + ", data.len=" + optString2.length() + "], costTime=" + currentTimeMillis2 + ", url=" + str); if (str.length() < 50) { str5 = str; } else { str5 = str.substring(0, 50); m.f(str5, "this as java.lang.String…ing(startIndex, endIndex)"); } qVar.g(19789, "ad_h5_intercept_result", optString, Integer.valueOf(optInt), Long.valueOf(currentTimeMillis2), str5); if (optInt == 0) { qVar.t(1951, 5); if (optString2.length() == 0) { z17 = true; } else { z17 = false; } if (!z17) { return c(optString2); } return null; } else if (optInt == 1) { qVar.t(1951, 8); return null; } else if (optInt == 101) { qVar.t(1951, 9); return null; } else if (optInt != 201) { qVar.t(1951, 10); return null; } else { b.f304408a.a(str); qVar.t(1951, 7); return null; } } else { Log.i("AdH5ContentFetcher", "fetchUrlResponse, content empty, costTime=" + (System.currentTimeMillis() - currentTimeMillis) + ", reqUrl=" + str3); return null; } } } catch (Throwable th8) { Log.e("AdH5ContentFetcher", "fetchUrlResponse, exp=" + th8 + ", url=" + str); return null; } } z16 = true; if (z16) { } } } catch (Throwable th9) { th6 = th9; try { Log.e("AdH5ContentFetcher", "fetchUrlContent, exp=" + th6 + ", url=" + str3); if (httpURLConnection != null) { } str4 = null; if (str4 != null) { } z16 = true; if (z16) { } } catch (Throwable th10) { Log.e("AdH5ContentFetcher", "fetchUrlContent, disconnect exp=" + th10 + ", url=" + str3); } } } if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Throwable th11) { th5 = th11; sb5 = new StringBuilder(); } } } catch (Throwable th12) { th6 = th12; httpURLConnection = null; Log.e("AdH5ContentFetcher", "fetchUrlContent, exp=" + th6 + ", url=" + str3); if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Throwable th13) { th5 = th13; sb5 = new StringBuilder(); } } str4 = null; if (str4 != null) { } z16 = true; if (z16) { } } str4 = null; if (str4 != null) { } z16 = true; if (z16) { } sb5.append("fetchUrlContent, disconnect exp="); sb5.append(th5); sb5.append(", url="); sb5.append(str3); Log.e("AdH5ContentFetcher", sb5.toString()); str4 = null; if (str4 != null) { } z16 = true; if (z16) { } throw th; } public final HttpURLConnection b(String str) { try { URLConnection openConnection = new URL(str).openConnection(); m.e(openConnection, "null cannot be cast to non-null type java.net.HttpURLConnection"); HttpURLConnection httpURLConnection = (HttpURLConnection) openConnection; httpURLConnection.setRequestMethod("GET"); httpURLConnection.setInstanceFollowRedirects(true); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); httpURLConnection.connect(); return httpURLConnection; } catch (Throwable th5) { Log.e("AdH5ContentFetcher", "getConnection, exp=" + th5); return null; } } /* JADX WARNING: Removed duplicated region for block: B:10:0x0010 A[Catch:{ all -> 0x000b }] */ /* JADX WARNING: Removed duplicated region for block: B:14:? A[RETURN, SYNTHETIC] */ public final WebResourceResponse c(String str) { boolean z15; if (str != null) { try { if (str.length() != 0) { z15 = false; if (!z15) { return null; } byte[] bytes = str.getBytes(c.f208058a); m.f(bytes, "this as java.lang.String).getBytes(charset)"); return new WebResourceResponse("text/html", "utf-8", new ByteArrayInputStream(bytes)); } } catch (Throwable th5) { Log.e("AdH5ContentFetcher", "getResponse, exp=" + th5); return null; } } z15 = true; if (!z15) { } } public final String d(InputStream inputStream) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream); byte[] bArr = new byte[1024]; while (true) { try { int read = inputStream.read(bArr); if (read <= 0) { break; } bufferedOutputStream.write(bArr, 0, read); } catch (Throwable th5) { try { bufferedOutputStream.close(); } catch (Exception e15) { Log.e("AdH5ContentFetcher", "readStringFromStream, bufferOs.close exp=" + e15); } try { inputStream.close(); } catch (Exception e16) { Log.e("AdH5ContentFetcher", "readStringFromStream, is.close exp=" + e16); } throw th5; } } bufferedOutputStream.flush(); String byteArrayOutputStream2 = byteArrayOutputStream.toString("UTF-8"); m.f(byteArrayOutputStream2, "byteOs.toString(\"UTF-8\")"); try { bufferedOutputStream.close(); } catch (Exception e17) { Log.e("AdH5ContentFetcher", "readStringFromStream, bufferOs.close exp=" + e17); } try { inputStream.close(); } catch (Exception e18) { Log.e("AdH5ContentFetcher", "readStringFromStream, is.close exp=" + e18); } return byteArrayOutputStream2; } }