导航菜单

页面标题

页面副标题

fieldd v4.2.4 - HttpPostService.java 源代码

正在查看: fieldd v4.2.4 应用的 HttpPostService.java JAVA 源代码文件

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


package com.marianhello.bgloc;

import io.grpc.internal.GrpcUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

public class HttpPostService {
    public static final int BUFFER_SIZE = 1024;
    private HttpURLConnection mHttpURLConnection;
    private String mUrl;

    public interface UploadingProgressListener {
        void onProgress(int i);
    }

    public HttpPostService(String str) {
        this.mUrl = str;
    }

    public HttpPostService(HttpURLConnection httpURLConnection) {
        this.mHttpURLConnection = httpURLConnection;
    }

    private HttpURLConnection openConnection() throws IOException {
        if (this.mHttpURLConnection == null) {
            this.mHttpURLConnection = (HttpURLConnection) new URL(this.mUrl).openConnection();
        }
        return this.mHttpURLConnection;
    }

    public int postJSON(JSONObject jSONObject, Map map) throws IOException {
        String str;
        if (jSONObject == null) {
            str = "null";
        } else {
            str = jSONObject.toString();
        }
        return postJSONString(str, map);
    }

    public int postJSON(JSONArray jSONArray, Map map) throws IOException {
        String str;
        if (jSONArray == null) {
            str = "null";
        } else {
            str = jSONArray.toString();
        }
        return postJSONString(str, map);
    }

    public int postJSONString(String str, Map map) throws IOException {
        OutputStreamWriter outputStreamWriter;
        if (map == null) {
            map = new HashMap();
        }
        HttpURLConnection openConnection = openConnection();
        openConnection.setDoOutput(true);
        openConnection.setFixedLengthStreamingMode(str.length());
        openConnection.setRequestMethod(GrpcUtil.HTTP_METHOD);
        openConnection.setRequestProperty("Content-Type", "application/json");
        for (Map.Entry entry : map.entrySet()) {
            openConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        OutputStreamWriter outputStreamWriter2 = null;
        try {
            outputStreamWriter = new OutputStreamWriter(openConnection.getOutputStream());
        } catch (Throwable th) {
            th = th;
        }
        try {
            outputStreamWriter.write(str);
            outputStreamWriter.flush();
            outputStreamWriter.close();
            return openConnection.getResponseCode();
        } catch (Throwable th2) {
            th = th2;
            outputStreamWriter2 = outputStreamWriter;
            if (outputStreamWriter2 != null) {
                outputStreamWriter2.flush();
                outputStreamWriter2.close();
            }
            throw th;
        }
    }

    public int postJSONFile(File file, Map map, UploadingProgressListener uploadingProgressListener) throws IOException {
        return postJSONFile(new FileInputStream(file), map, uploadingProgressListener);
    }

    public int postJSONFile(InputStream inputStream, Map map, UploadingProgressListener uploadingProgressListener) throws IOException {
        BufferedInputStream bufferedInputStream;
        if (map == null) {
            map = new HashMap();
        }
        long available = inputStream.available();
        HttpURLConnection openConnection = openConnection();
        openConnection.setDoInput(false);
        openConnection.setDoOutput(true);
        openConnection.setFixedLengthStreamingMode(available);
        openConnection.setRequestMethod(GrpcUtil.HTTP_METHOD);
        openConnection.setRequestProperty("Content-Type", "application/json");
        for (Map.Entry entry : map.entrySet()) {
            openConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        byte[] bArr = new byte[1024];
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(inputStream);
            try {
                BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(openConnection.getOutputStream());
                long j = 0;
                while (true) {
                    try {
                        int read = bufferedInputStream.read(bArr);
                        if (read != -1) {
                            bufferedOutputStream2.write(bArr, 0, read);
                            bufferedOutputStream2.flush();
                            j += read;
                            int i = (int) ((100 * j) / available);
                            if (uploadingProgressListener != null) {
                                uploadingProgressListener.onProgress(i);
                            }
                        } else {
                            bufferedOutputStream2.flush();
                            bufferedOutputStream2.close();
                            bufferedInputStream.close();
                            return openConnection.getResponseCode();
                        }
                    } catch (Throwable th) {
                        th = th;
                        bufferedOutputStream = bufferedOutputStream2;
                        if (bufferedOutputStream != null) {
                            bufferedOutputStream.flush();
                            bufferedOutputStream.close();
                        }
                        if (bufferedInputStream != null) {
                            bufferedInputStream.close();
                        }
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Throwable th3) {
            th = th3;
            bufferedInputStream = null;
        }
    }

    public static int postJSON(String str, JSONObject jSONObject, Map map) throws IOException {
        return new HttpPostService(str).postJSON(jSONObject, map);
    }

    public static int postJSON(String str, JSONArray jSONArray, Map map) throws IOException {
        return new HttpPostService(str).postJSON(jSONArray, map);
    }

    public static int postJSONFile(String str, File file, Map map, UploadingProgressListener uploadingProgressListener) throws IOException {
        return new HttpPostService(str).postJSONFile(file, map, uploadingProgressListener);
    }
}