package cn.com.magnity.magnitycx.upgrade; import android.os.Handler; import cn.com.magnity.magnitycx.MagApplication; import cn.com.magnity.magnitycx.log.Logging; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; /* loaded from: classes.dex */ public class HttpHelper { public static volatile boolean isCancelled; public static String GetRemoteVersionInfo(String path, int connTimeout, int readTimeout) throws Exception { String str; BufferedReader br = null; InputStream is = null; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(connTimeout); conn.setReadTimeout(readTimeout); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Connextion", "Keep-Alive"); int code = conn.getResponseCode(); if (code == 302) { URL url2 = new URL(conn.getHeaderField("Location")); conn = (HttpURLConnection) url2.openConnection(); conn.setConnectTimeout(connTimeout); conn.setReadTimeout(readTimeout); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Connextion", "Keep-Alive"); code = conn.getResponseCode(); } if (code == 200) { is = conn.getInputStream(); BufferedReader br2 = new BufferedReader(new InputStreamReader(is)); try { StringBuffer stringBuffer = new StringBuffer(""); String newLine = System.getProperty("line.separator"); while (true) { String line = br2.readLine(); if (line == null) { break; } stringBuffer.append(line + newLine); } if (stringBuffer.length() > 0) { stringBuffer.deleteCharAt(stringBuffer.length() - 1); } br2.close(); is.close(); str = stringBuffer.toString(); if (br2 != null) { try { br2.close(); } catch (IOException e) { Logging.error("Fail to get remote info because of io error."); } } if (is != null) { is.close(); } } catch (Throwable th) { th = th; br = br2; if (br != null) { try { br.close(); } catch (IOException e2) { Logging.error("Fail to get remote info because of io error."); throw th; } } if (is != null) { is.close(); } throw th; } } else { Logging.error("Get error respond(" + code + ")"); str = ""; if (0 != 0) { try { br.close(); } catch (IOException e3) { Logging.error("Fail to get remote info because of io error."); } } if (0 != 0) { is.close(); } } return str; } catch (Throwable th2) { th = th2; } } public static File getRemoteApk(String path, String version, int connTimeout, int readTimeout, Handler handler) throws Exception { File file; FileOutputStream fos; int total; int len; FileOutputStream fos2 = null; BufferedInputStream bis = null; InputStream is = null; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(connTimeout); conn.setReadTimeout(readTimeout); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Connextion", "Keep-Alive"); int code = conn.getResponseCode(); if (code == 302) { URL url2 = new URL(conn.getHeaderField("Location")); conn = (HttpURLConnection) url2.openConnection(); conn.setConnectTimeout(connTimeout); conn.setReadTimeout(readTimeout); conn.setRequestProperty("Accept-Encoding", "identity"); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Connextion", "Keep-Alive"); code = conn.getResponseCode(); } if (code == 200) { int size = conn.getContentLength(); handler.sendMessage(handler.obtainMessage(1, size, 0)); is = conn.getInputStream(); BufferedInputStream bis2 = new BufferedInputStream(is); try { file = new File(MagApplication.magParameter.otherDir, "MAG-Cx." + version + ".apk"); if (file.exists()) { file.delete(); } fos = new FileOutputStream(file); total = 0; } catch (Throwable th) { th = th; bis = bis2; } try { byte[] buf = new byte[1024]; while (!isCancelled && (len = bis2.read(buf)) != -1) { fos.write(buf, 0, len); total += len; handler.sendMessage(handler.obtainMessage(2, total, 0)); } fos.close(); bis2.close(); is.close(); if (total != size) { file = null; } if (fos != null) { try { fos.close(); } catch (IOException e) { Logging.error("Fail to get remote file because of io error."); } } if (bis2 != null) { bis2.close(); } if (is != null) { is.close(); } } catch (Throwable th2) { th = th2; bis = bis2; fos2 = fos; if (fos2 != null) { try { fos2.close(); } catch (IOException e2) { Logging.error("Fail to get remote file because of io error."); throw th; } } if (bis != null) { bis.close(); } if (is != null) { is.close(); } throw th; } } else { Logging.error("Get error respond(" + code + ")"); file = null; if (0 != 0) { try { fos2.close(); } catch (IOException e3) { Logging.error("Fail to get remote file because of io error."); } } if (0 != 0) { bis.close(); } if (0 != 0) { is.close(); } } return file; } catch (Throwable th3) { th = th3; } } }