package com.ironsource.mediationsdk.server; import android.text.TextUtils; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import com.ironsource.i9; import com.ironsource.mediationsdk.logger.IronLog; import com.ironsource.mediationsdk.p; import com.ironsource.pd; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /* loaded from: classes4.dex */ public class HttpFunctions { public static final String ERROR_PREFIX = "ERROR:"; private static final int a = 15000; private static final String b = "GET"; private static final String c = "POST"; private static final String d = "UTF-8"; private static final String e = "Bad Request - 400"; private static final ExecutorService f = Executors.newSingleThreadExecutor(); public class a implements Runnable { final /* synthetic */ String a; final /* synthetic */ String b; final /* synthetic */ pd c; public a(String str, String str2, pd pdVar) { this.a = str; this.b = str2; this.c = pdVar; } @Override // java.lang.Runnable public void run() { HttpURLConnection httpURLConnection; OutputStream outputStream; try { httpURLConnection = HttpFunctions.b(this.a); } catch (Exception e) { e = e; httpURLConnection = null; outputStream = null; } catch (Throwable th) { th = th; httpURLConnection = null; outputStream = null; } try { outputStream = httpURLConnection.getOutputStream(); try { try { HttpFunctions.b(this.b, outputStream); int responseCode = httpURLConnection.getResponseCode(); boolean z = responseCode == 200; if (!z) { IronLog.INTERNAL.error("invalid response code " + responseCode + " sending request"); } this.c.a(z); } catch (Exception e2) { e = e2; i9.d().a(e); IronLog.INTERNAL.error("exception while sending request " + e.getMessage()); this.c.a(false); HttpFunctions.b(outputStream, httpURLConnection, null); } } catch (Throwable th2) { th = th2; HttpFunctions.b(outputStream, httpURLConnection, null); throw th; } } catch (Exception e3) { e = e3; outputStream = null; } catch (Throwable th3) { th = th3; outputStream = null; HttpFunctions.b(outputStream, httpURLConnection, null); throw th; } HttpFunctions.b(outputStream, httpURLConnection, null); } } private static String a(BufferedReader bufferedReader) throws IOException { StringBuilder sb = new StringBuilder(); while (true) { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } sb.append(readLine); } String sb2 = sb.toString(); if (TextUtils.isEmpty(sb2)) { return null; } return sb2; } /* JADX INFO: Access modifiers changed from: private */ public static HttpURLConnection b(String str) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(new URL(str).openConnection())); httpURLConnection.setReadTimeout(15000); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); return httpURLConnection; } /* JADX INFO: Access modifiers changed from: private */ public static void b(OutputStream outputStream, HttpURLConnection httpURLConnection, BufferedReader bufferedReader) { if (outputStream != null) { try { outputStream.close(); } catch (IOException e2) { i9.d().a(e2); IronLog.INTERNAL.error("exception while closing output stream " + e2.getMessage()); } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e3) { i9.d().a(e3); IronLog.INTERNAL.error("exception while closing reader " + e3.getMessage()); } } } /* JADX INFO: Access modifiers changed from: private */ public static void b(String str, OutputStream outputStream) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); bufferedWriter.write(str); bufferedWriter.flush(); bufferedWriter.close(); } public static String getStringFromURL(String str) throws Exception { return getStringFromURL(str, null); } public static String getStringFromURL(String str, p.c cVar) { HttpURLConnection httpURLConnection; BufferedReader bufferedReader; Exception e2; BufferedReader bufferedReader2; Throwable th; try { httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(new URL(str).openConnection())); } catch (Exception e3) { e = e3; httpURLConnection = null; } catch (Throwable th2) { th = th2; httpURLConnection = null; bufferedReader = null; } try { httpURLConnection.setReadTimeout(15000); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true); httpURLConnection.connect(); if (httpURLConnection.getResponseCode() == 400) { if (cVar != null) { cVar.a(e); } b(null, httpURLConnection, null); return null; } bufferedReader2 = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); try { String a2 = a(bufferedReader2); b(null, httpURLConnection, bufferedReader2); return a2; } catch (Exception e4) { e2 = e4; try { i9.d().a(e2); b(null, httpURLConnection, bufferedReader2); return null; } catch (Throwable th3) { bufferedReader = bufferedReader2; th = th3; BufferedReader bufferedReader3 = bufferedReader; th = th; bufferedReader2 = bufferedReader3; b(null, httpURLConnection, bufferedReader2); throw th; } } catch (Throwable th4) { th = th4; b(null, httpURLConnection, bufferedReader2); throw th; } } catch (Exception e5) { e = e5; e2 = e; bufferedReader2 = null; i9.d().a(e2); b(null, httpURLConnection, bufferedReader2); return null; } catch (Throwable th5) { th = th5; bufferedReader = null; BufferedReader bufferedReader32 = bufferedReader; th = th; bufferedReader2 = bufferedReader32; b(null, httpURLConnection, bufferedReader2); throw th; } } public static String sendPostRequest(String str, String str2, p.c cVar) { Throwable th; BufferedReader bufferedReader; OutputStream outputStream; Exception e2; HttpURLConnection httpURLConnection; HttpURLConnection httpURLConnection2 = null; try { httpURLConnection = b(str); try { httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); outputStream = httpURLConnection.getOutputStream(); try { b(str2, outputStream); int responseCode = httpURLConnection.getResponseCode(); if (responseCode != 200) { if (responseCode == 400 && cVar != null) { cVar.a(e); } b(outputStream, httpURLConnection, null); return null; } bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); try { String a2 = a(bufferedReader); b(outputStream, httpURLConnection, bufferedReader); return a2; } catch (Exception e3) { e2 = e3; try { i9.d().a(e2); IronLog.INTERNAL.error("exception while sending request " + e2.getMessage()); b(outputStream, httpURLConnection, bufferedReader); return null; } catch (Throwable th2) { th = th2; httpURLConnection2 = httpURLConnection; httpURLConnection = httpURLConnection2; b(outputStream, httpURLConnection, bufferedReader); throw th; } } catch (Throwable th3) { th = th3; b(outputStream, httpURLConnection, bufferedReader); throw th; } } catch (Exception e4) { e2 = e4; bufferedReader = null; i9.d().a(e2); IronLog.INTERNAL.error("exception while sending request " + e2.getMessage()); b(outputStream, httpURLConnection, bufferedReader); return null; } catch (Throwable th4) { th = th4; bufferedReader = null; httpURLConnection2 = httpURLConnection; httpURLConnection = httpURLConnection2; b(outputStream, httpURLConnection, bufferedReader); throw th; } } catch (Exception e5) { e2 = e5; outputStream = null; } catch (Throwable th5) { th = th5; bufferedReader = null; outputStream = null; } } catch (Exception e6) { e2 = e6; httpURLConnection = null; outputStream = null; } catch (Throwable th6) { th = th6; bufferedReader = null; outputStream = null; httpURLConnection = httpURLConnection2; b(outputStream, httpURLConnection, bufferedReader); throw th; } } public static void sendPostRequest(String str, String str2, pd pdVar) { f.submit(new a(str, str2, pdVar)); } }