导航菜单

页面标题

页面副标题

Oolka v2.4.42 - GeneralHttpService.java 源代码

正在查看: Oolka v2.4.42 应用的 GeneralHttpService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.singular.sdk.internal;

import android.net.Uri;
import com.singular.sdk.internal.GeneralHttpServiceBase;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONObject;

public class GeneralHttpService extends GeneralHttpServiceBase {
    private static final String BASE_URL = "https://sdk-api-v1.singular.net/api/v1";
    private final SingularLog logger;

    public GeneralHttpService() {
        super("https://sdk-api-v1.singular.net/api/v1");
        this.logger = SingularLog.getLogger(GeneralHttpService.class.getSimpleName());
    }

    private HttpURLConnection buildRequest(String str, Map<String, String> map) {
        try {
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) new URL(str).openConnection();
            httpsURLConnection.setConnectTimeout(10000);
            httpsURLConnection.setReadTimeout(10000);
            httpsURLConnection.setRequestMethod("POST");
            httpsURLConnection.setDoInput(true);
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setUseCaches(false);
            httpsURLConnection.setRequestProperty("User-Agent", Constants.HTTP_USER_AGENT);
            httpsURLConnection.setRequestProperty("Content-Type", "application/json");
            httpsURLConnection.setRequestProperty("Accept-Encoding", "gzip");
            try {
                JSONObject jSONObject = map != null ? new JSONObject(map) : new JSONObject();
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpsURLConnection.getOutputStream(), "UTF-8");
                outputStreamWriter.write(jSONObject.toString());
                outputStreamWriter.close();
                return httpsURLConnection;
            } catch (Throwable th) {
                this.logger.error("Error in JSON Serialization ", th);
                this.logger.error(Utils.formatException(th));
                return null;
            }
        } catch (Throwable th2) {
            this.logger.error(Utils.formatException(th2));
            return null;
        }
    }

    @Override
    public void sendRequest(final String str, final Map<String, String> map, final Map<String, String> map2, final GeneralHttpServiceBase.CompletionHandler completionHandler) {
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                GeneralHttpService.this.sendSynchronousRequest(str, map, map2, completionHandler);
            }
        });
    }

    public void sendSynchronousRequest(String str, Map<String, String> map, Map<String, String> map2, GeneralHttpServiceBase.CompletionHandler completionHandler) {
        HttpURLConnection httpURLConnection;
        String str2 = "?a=" + SingularInstance.getInstance().getSingularConfig().apiKey;
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                str2 = str2 + "&" + Uri.encode(entry.getKey()) + "=" + Uri.encode(entry.getValue());
            }
        }
        try {
            httpURLConnection = buildRequest(getBaseUrl() + str + (str2 + "&h=" + Utils.sha1Hash(str2, SingularInstance.getInstance().getSingularConfig().secret)), map2);
            try {
            } catch (Throwable th) {
                th = th;
                try {
                    completionHandler.onFailure("Error sending request: message - " + th.getMessage());
                    this.logger.error(Utils.formatException(th));
                    if (httpURLConnection == null) {
                        return;
                    }
                    httpURLConnection.disconnect();
                } finally {
                    if (httpURLConnection != null) {
                        httpURLConnection.disconnect();
                    }
                }
            }
        } catch (Throwable th2) {
            th = th2;
            httpURLConnection = null;
        }
        if (httpURLConnection == null) {
            completionHandler.onFailure("Error sending request: connection is null");
            this.logger.error("Error sending request: connection is null");
            if (httpURLConnection != null) {
                return;
            } else {
                return;
            }
        }
        httpURLConnection.connect();
        int responseCode = httpURLConnection.getResponseCode();
        StringBuffer stringBuffer = new StringBuffer();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader((httpURLConnection.getContentEncoding() == null || !httpURLConnection.getContentEncoding().equals("gzip")) ? new InputStreamReader(inputStream) : new InputStreamReader(new GZIPInputStream(inputStream)));
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                break;
            } else {
                stringBuffer.append(readLine);
            }
        }
        completionHandler.onSuccess(stringBuffer.toString(), responseCode);
        httpURLConnection.disconnect();
    }
}