b.java 源代码


package com.pgl.a.a;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import com.x8zs.plugin.apache.http.client.methods.HttpDelete;
import com.x8zs.plugin.apache.http.client.methods.HttpHead;
import com.x8zs.plugin.apache.http.client.methods.HttpPut;
import com.x8zs.plugin.apache.http.client.methods.HttpTrace;
import com.x8zs.plugin.apache.http.cookie.SM;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.Locale;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;

public abstract class b {

    private Context f20058b;

    private String f20059c;

    private int f20060d;

    private int f20061e;

    private byte[] f20062f;

    private HandlerThread f20069m;

    private Handler f20070n;

    public String f20057a = "";

    private int f20063g = -1;

    private byte[] f20064h = null;

    private int f20065i = 10000;

    private int f20066j = 1;

    private int f20067k = 0;

    private boolean f20068l = false;

    private boolean f20071o = false;

    private HttpURLConnection f20072p = null;

    class a implements Handler.Callback {
        a() {
        }

        @Override
        public boolean handleMessage(Message message) {
            if (b.this.f20067k >= b.this.f20066j) {
                b.this.f20068l = false;
                b.this.f20067k = 0;
                b.this.f20069m.quit();
                return false;
            }
            if (b.this.a()) {
                b.this.f20068l = false;
                b.this.f20067k = 0;
                b.this.f20069m.quit();
                return true;
            }
            b.this.f20070n.sendEmptyMessageDelayed(1, b.this.f20065i * 1);
            b.g(b.this);
            return false;
        }
    }

    public b(Context context, String str) {
        str = (str == null || str.length() <= 0) ? "" : str;
        this.f20058b = context;
        this.f20059c = str;
    }

    static int g(b bVar) {
        int i6 = bVar.f20067k;
        bVar.f20067k = i6 + 1;
        return i6;
    }

    public abstract boolean a(int i6, byte[] bArr);

    private void b(int i6) {
        HttpURLConnection httpURLConnection;
        StringBuilder sb;
        String str = i6 != 1 ? i6 != 2 ? "" : "application/octet-stream" : "application/json; charset=utf-8";
        if (str.length() > 0) {
            this.f20072p.addRequestProperty("Content-Type", str);
        }
        this.f20072p.addRequestProperty(SM.COOKIE, "sessionid=" + this.f20059c);
        try {
            String language = Locale.getDefault().getLanguage();
            if (language.equalsIgnoreCase("zh")) {
                httpURLConnection = this.f20072p;
                sb = new StringBuilder();
                sb.append(Locale.getDefault().toString());
                sb.append(",");
                sb.append(language);
                sb.append(";q=0.9");
            } else {
                httpURLConnection = this.f20072p;
                sb = new StringBuilder();
                sb.append(Locale.getDefault().toString());
                sb.append(",");
                sb.append(language);
                sb.append(";q=0.9,en-US;q=0.6,en;q=0.4");
            }
            httpURLConnection.addRequestProperty("Accept-Language", sb.toString());
        } catch (Throwable unused) {
        }
    }

    private void a(int i6) {
        String str = "GET";
        switch (i6) {
            case 1:
                str = "POST";
                break;
            case 3:
                str = HttpPut.METHOD_NAME;
                break;
            case 4:
                str = HttpDelete.METHOD_NAME;
                break;
            case 5:
                str = HttpHead.METHOD_NAME;
                break;
            case 6:
                str = HttpTrace.METHOD_NAME;
                break;
        }
        this.f20072p.setRequestMethod(str);
    }

    private void b(int i6, int i7, byte[] bArr) {
        this.f20060d = i6;
        this.f20061e = i7;
        this.f20062f = bArr;
    }

    public synchronized void a(int i6, int i7, byte[] bArr) {
        if (this.f20068l) {
            return;
        }
        this.f20066j = 2;
        b(i6, i7, bArr);
        this.f20068l = true;
        this.f20067k = 0;
        HandlerThread handlerThread = new HandlerThread("request");
        this.f20069m = handlerThread;
        handlerThread.start();
        Handler handler = new Handler(this.f20069m.getLooper(), new a());
        this.f20070n = handler;
        handler.sendEmptyMessage(1);
    }

    public boolean a() {
        boolean z6;
        HttpURLConnection httpURLConnection;
        InputStream inputStream;
        try {
            URL url = new URL(this.f20057a);
            if (this.f20071o) {
                HttpsURLConnection.setDefaultSSLSocketFactory(SSLContext.getInstance("TLS").getSocketFactory());
                httpURLConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
            } else {
                httpURLConnection = (HttpURLConnection) url.openConnection();
            }
            this.f20072p = httpURLConnection;
            this.f20072p.setConnectTimeout(this.f20065i);
            this.f20072p.setReadTimeout(this.f20065i);
            a(this.f20060d);
            b(this.f20061e);
            byte[] bArr = this.f20062f;
            z6 = true;
            if (bArr != null && bArr.length > 0) {
                this.f20072p.setDoOutput(true);
                OutputStream outputStream = this.f20072p.getOutputStream();
                outputStream.write(this.f20062f);
                outputStream.flush();
                outputStream.close();
            }
            this.f20072p.connect();
            try {
                this.f20063g = this.f20072p.getResponseCode();
                inputStream = this.f20072p.getInputStream();
                try {
                    this.f20064h = a(inputStream);
                } catch (Throwable unused) {
                }
            } catch (Throwable unused2) {
                inputStream = null;
            }
        } catch (Throwable th) {
            try {
                th.printStackTrace();
                HttpURLConnection httpURLConnection2 = this.f20072p;
                if (httpURLConnection2 != null) {
                    httpURLConnection2.disconnect();
                    this.f20072p = null;
                }
                z6 = false;
            } catch (Throwable th2) {
                if (this.f20072p != null) {
                    this.f20072p.disconnect();
                    this.f20072p = null;
                }
                throw th2;
            }
        }
        if (z6) {
            a(this.f20063g, this.f20064h);
        }
        return z6;
    }

    private byte[] a(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[1024];
        while (true) {
            int read = inputStream.read(bArr, 0, 1024);
            if (read <= 0) {
                return byteArrayOutputStream.toByteArray();
            }
            byteArrayOutputStream.write(bArr, 0, read);
        }
    }
}