导航菜单

页面标题

页面副标题

CreditNow v1.0.2 - HttpRequestBuilder.java 源代码

正在查看: CreditNow v1.0.2 应用的 HttpRequestBuilder.java JAVA 源代码文件

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


package com.regula.common.http;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.zip.GZIPInputStream;

public class HttpRequestBuilder {
    public static c n = c.a;
    public final URL b;
    public final String c;
    public e d;
    public String k;
    public int l;
    public boolean m;
    public HttpURLConnection a = null;
    public boolean e = true;
    public boolean f = false;
    public int g = 8192;
    public long h = -1;
    public long i = 0;
    public f j = f.a;

    public static class HttpRequestException extends RuntimeException {
        private static final long serialVersionUID = -1170466989781746231L;

        public HttpRequestException(IOException iOException) {
            super(iOException);
        }

        @Override
        public IOException getCause() {
            return (IOException) super.getCause();
        }
    }

    public class a extends b<HttpRequestBuilder> {
        public final InputStream c;
        public final OutputStream d;

        public a(Closeable closeable, boolean z, InputStream inputStream, OutputStream outputStream) {
            super(closeable, z);
            this.c = inputStream;
            this.d = outputStream;
        }

        @Override
        public HttpRequestBuilder b() throws IOException {
            byte[] bArr = new byte[HttpRequestBuilder.this.g];
            while (true) {
                int read = this.c.read(bArr);
                if (read == -1) {
                    return HttpRequestBuilder.this;
                }
                this.d.write(bArr, 0, read);
                HttpRequestBuilder.d(HttpRequestBuilder.this, read);
                HttpRequestBuilder.this.j.a(HttpRequestBuilder.this.i, HttpRequestBuilder.this.h);
            }
        }
    }

    public static abstract class b<V> extends d<V> {
        public final Closeable a;
        public final boolean b;

        public b(Closeable closeable, boolean z) {
            this.a = closeable;
            this.b = z;
        }

        @Override
        public void a() throws IOException {
            Closeable closeable = this.a;
            if (closeable instanceof Flushable) {
                ((Flushable) closeable).flush();
            }
            if (!this.b) {
                this.a.close();
            } else {
                try {
                    this.a.close();
                } catch (IOException unused) {
                }
            }
        }
    }

    public interface c {
        public static final c a = new a();

        public class a implements c {
            @Override
            public HttpURLConnection a(URL url, Proxy proxy) throws IOException {
                return (HttpURLConnection) url.openConnection(proxy);
            }

            @Override
            public HttpURLConnection b(URL url) throws IOException {
                return (HttpURLConnection) url.openConnection();
            }
        }

        HttpURLConnection a(URL url, Proxy proxy) throws IOException;

        HttpURLConnection b(URL url) throws IOException;
    }

    public static abstract class d<V> implements Callable<V> {
        public abstract void a() throws IOException;

        public abstract V b() throws HttpRequestException, IOException;

        @Override
        public V call() throws HttpRequestException {
            boolean z;
            try {
                try {
                    V b = b();
                    try {
                        a();
                        return b;
                    } catch (IOException e) {
                        throw new HttpRequestException(e);
                    }
                } catch (Throwable th) {
                    z = true;
                    th = th;
                    try {
                        a();
                    } catch (IOException e2) {
                        if (!z) {
                            throw new HttpRequestException(e2);
                        }
                    }
                    throw th;
                }
            } catch (HttpRequestException e3) {
                throw e3;
            } catch (IOException e4) {
                throw new HttpRequestException(e4);
            } catch (Throwable th2) {
                th = th2;
                z = false;
                a();
                throw th;
            }
        }
    }

    public static class e extends BufferedOutputStream {
        public final CharsetEncoder a;

        public e(OutputStream outputStream, String str, int i) {
            super(outputStream, i);
            this.a = Charset.forName(HttpRequestBuilder.w(str)).newEncoder();
        }

        public e a(String str) throws IOException {
            ByteBuffer encode = this.a.encode(CharBuffer.wrap(str));
            super.write(encode.array(), 0, encode.limit());
            return this;
        }
    }

    public HttpRequestBuilder(CharSequence charSequence, String str) throws HttpRequestException {
        try {
            this.b = new URL(charSequence.toString());
            this.c = str;
        } catch (MalformedURLException e2) {
            throw new HttpRequestException(e2);
        }
    }

    public static HttpRequestBuilder G(CharSequence charSequence) throws HttpRequestException {
        return new HttpRequestBuilder(charSequence, "POST");
    }

    public static HttpRequestBuilder I(CharSequence charSequence) throws HttpRequestException {
        return new HttpRequestBuilder(charSequence, "PUT");
    }

    public static long d(HttpRequestBuilder httpRequestBuilder, long j) {
        long j2 = httpRequestBuilder.i + j;
        httpRequestBuilder.i = j2;
        return j2;
    }

    public static HttpRequestBuilder r(CharSequence charSequence) throws HttpRequestException {
        return new HttpRequestBuilder(charSequence, "DELETE");
    }

    public static HttpRequestBuilder t(CharSequence charSequence) throws HttpRequestException {
        return new HttpRequestBuilder(charSequence, "GET");
    }

    public static String w(String str) {
        return (str == null || str.length() <= 0) ? "UTF-8" : str;
    }

    public HttpRequestBuilder A(Map<String, String> map) {
        if (map != null && !map.isEmpty()) {
            Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                y(it.next());
            }
        }
        return this;
    }

    public final HttpRequestBuilder B(long j) {
        if (this.h == -1) {
            this.h = 0L;
        }
        this.h += j;
        return this;
    }

    public int C(String str) throws HttpRequestException {
        return D(str, -1);
    }

    public int D(String str, int i) throws HttpRequestException {
        k();
        return u().getHeaderFieldInt(str, i);
    }

    public String E() {
        return u().getRequestMethod();
    }

    public HttpRequestBuilder F() throws IOException {
        if (this.d != null) {
            return this;
        }
        try {
            u().setDoOutput(true);
        } catch (Exception unused) {
        }
        this.d = new e(u().getOutputStream(), v(u().getRequestProperty("Content-Type"), "charset"), this.g);
        return this;
    }

    public HttpRequestBuilder H(f fVar) {
        if (fVar == null) {
            this.j = f.a;
        } else {
            this.j = fVar;
        }
        return this;
    }

    public InputStreamReader J(String str) throws HttpRequestException {
        try {
            return new InputStreamReader(N(), w(str));
        } catch (UnsupportedEncodingException e2) {
            throw new HttpRequestException(e2);
        }
    }

    public String K(HttpRequestBuilder httpRequestBuilder) {
        BufferedReader bufferedReader = new BufferedReader(httpRequestBuilder.J("UTF-8"));
        try {
            StringBuilder sb = new StringBuilder();
            while (true) {
                int read = bufferedReader.read();
                if (read < 0) {
                    return sb.toString();
                }
                sb.append((char) read);
            }
        } catch (Exception unused) {
            return "";
        }
    }

    public HttpRequestBuilder L(InputStream inputStream) throws HttpRequestException {
        try {
            F();
            o(inputStream, this.d);
            return this;
        } catch (IOException e2) {
            throw new HttpRequestException(e2);
        }
    }

    public HttpRequestBuilder M(byte[] bArr) throws HttpRequestException {
        if (bArr != null) {
            B(bArr.length);
        }
        return L(new ByteArrayInputStream(bArr));
    }

    public InputStream N() throws HttpRequestException {
        InputStream inputStream;
        if (l() < 400) {
            try {
                inputStream = u().getInputStream();
            } catch (IOException e2) {
                throw new HttpRequestException(e2);
            }
        } else {
            inputStream = u().getErrorStream();
            if (inputStream == null) {
                try {
                    inputStream = u().getInputStream();
                } catch (IOException e3) {
                    if (n() > 0) {
                        throw new HttpRequestException(e3);
                    }
                    inputStream = new ByteArrayInputStream(new byte[0]);
                }
            }
        }
        if (!this.f || !"gzip".equals(m())) {
            return inputStream;
        }
        try {
            return new GZIPInputStream(inputStream);
        } catch (IOException e4) {
            throw new HttpRequestException(e4);
        }
    }

    public URL O() {
        return u().getURL();
    }

    public BufferedInputStream g() throws HttpRequestException {
        return new BufferedInputStream(N(), this.g);
    }

    public ByteArrayOutputStream h() {
        int n2 = n();
        return n2 > 0 ? new ByteArrayOutputStream(n2) : new ByteArrayOutputStream();
    }

    public byte[] i() throws HttpRequestException {
        ByteArrayOutputStream h = h();
        try {
            o(g(), h);
            return h.toByteArray();
        } catch (IOException e2) {
            throw new HttpRequestException(e2);
        }
    }

    public HttpRequestBuilder j() throws IOException {
        H(null);
        e eVar = this.d;
        if (eVar == null) {
            return this;
        }
        if (this.m) {
            eVar.a("\r\n--00content0boundary00--\r\n");
        }
        if (this.e) {
            try {
                this.d.close();
            } catch (IOException unused) {
            }
        } else {
            this.d.close();
        }
        this.d = null;
        return this;
    }

    public HttpRequestBuilder k() throws HttpRequestException {
        try {
            return j();
        } catch (IOException e2) {
            throw new HttpRequestException(e2);
        }
    }

    public int l() throws HttpRequestException {
        try {
            j();
            return u().getResponseCode();
        } catch (IOException e2) {
            throw new HttpRequestException(e2);
        }
    }

    public String m() {
        return z("Content-Encoding");
    }

    public int n() {
        return C("Content-Length");
    }

    public HttpRequestBuilder o(InputStream inputStream, OutputStream outputStream) throws IOException {
        return new a(inputStream, this.e, inputStream, outputStream).call();
    }

    public final HttpURLConnection p() {
        try {
            HttpURLConnection a2 = this.k != null ? n.a(this.b, q()) : n.b(this.b);
            a2.setRequestMethod(this.c);
            return a2;
        } catch (IOException e2) {
            throw new HttpRequestException(e2);
        }
    }

    public final Proxy q() {
        return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.k, this.l));
    }

    public HttpRequestBuilder s() {
        u().disconnect();
        return this;
    }

    public String toString() {
        return E() + ' ' + O();
    }

    public HttpURLConnection u() {
        if (this.a == null) {
            this.a = p();
        }
        return this.a;
    }

    public String v(String str, String str2) {
        String trim;
        int length;
        if (str != null && str.length() != 0) {
            int length2 = str.length();
            int indexOf = str.indexOf(59) + 1;
            if (indexOf != 0 && indexOf != length2) {
                int indexOf2 = str.indexOf(59, indexOf);
                if (indexOf2 == -1) {
                    indexOf2 = length2;
                }
                while (indexOf < indexOf2) {
                    int indexOf3 = str.indexOf(61, indexOf);
                    if (indexOf3 != -1 && indexOf3 < indexOf2 && str2.equals(str.substring(indexOf, indexOf3).trim()) && (length = (trim = str.substring(indexOf3 + 1, indexOf2).trim()).length()) != 0) {
                        if (length > 2 && '\"' == trim.charAt(0)) {
                            int i = length - 1;
                            if ('\"' == trim.charAt(i)) {
                                return trim.substring(1, i);
                            }
                        }
                        return trim;
                    }
                    indexOf = indexOf2 + 1;
                    indexOf2 = str.indexOf(59, indexOf);
                    if (indexOf2 == -1) {
                        indexOf2 = length2;
                    }
                }
            }
        }
        return null;
    }

    public HttpRequestBuilder x(String str, String str2) {
        u().setRequestProperty(str, str2);
        return this;
    }

    public HttpRequestBuilder y(Map.Entry<String, String> entry) {
        return x(entry.getKey(), entry.getValue());
    }

    public String z(String str) throws HttpRequestException {
        k();
        return u().getHeaderField(str);
    }

    public interface f {
        public static final f a = new a();

        void a(long j, long j2);

        public class a implements f {
            @Override
            public void a(long j, long j2) {
            }
        }
    }
}