Request.java

com.mojang.realmsclient.client.Request

信息

  • 全限定名:com.mojang.realmsclient.client.Request
  • 类型:public abstract class
  • 包:com.mojang.realmsclient.client
  • 源码路径:src/main/java/com/mojang/realmsclient/client/Request.java
  • 起始行号:L19
  • 职责:

    TODO

字段/常量

  • connection

    • 类型: HttpURLConnection
    • 修饰符: protected
    • 源码定位: L20
    • 说明:

      TODO

  • connected

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L21
    • 说明:

      TODO

  • url

    • 类型: String
    • 修饰符: protected
    • 源码定位: L22
    • 说明:

      TODO

  • DEFAULT_READ_TIMEOUT

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L23
    • 说明:

      TODO

  • DEFAULT_CONNECT_TIMEOUT

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L24
    • 说明:

      TODO

  • IS_SNAPSHOT_KEY

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L25
    • 说明:

      TODO

  • COOKIE_KEY

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L26
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.client.Request.Delete

    • 类型: class
    • 修饰符: public static
    • 源码定位: L203
    • 说明:

      TODO

  • com.mojang.realmsclient.client.Request.Get

    • 类型: class
    • 修饰符: public static
    • 源码定位: L221
    • 说明:

      TODO

  • com.mojang.realmsclient.client.Request.Post

    • 类型: class
    • 修饰符: public static
    • 源码定位: L240
    • 说明:

      TODO

  • com.mojang.realmsclient.client.Request.Put

    • 类型: class
    • 修饰符: public static
    • 源码定位: L271
    • 说明:

      TODO

构造器

public Request(String url, int connectTimeout, int readTimeout) @ L28

  • 构造器名:Request
  • 源码定位:L28
  • 修饰符:public

参数:

  • url: String
  • connectTimeout: int
  • readTimeout: int

说明:

TODO

方法

下面的方法块按源码顺序生成。

public void cookie(String key, String value) @ L47

  • 方法名:cookie
  • 源码定位:L47
  • 返回类型:void
  • 修饰符:public

参数:

  • key: String
  • value: String

说明:

TODO

public static void cookie(HttpURLConnection connection, String key, String value) @ L51

  • 方法名:cookie
  • 源码定位:L51
  • 返回类型:void
  • 修饰符:public static

参数:

  • connection: HttpURLConnection
  • key: String
  • value: String

说明:

TODO

public void addSnapshotHeader(boolean isSnapshot) @ L60

  • 方法名:addSnapshotHeader
  • 源码定位:L60
  • 返回类型:void
  • 修饰符:public

参数:

  • isSnapshot: boolean

说明:

TODO

public int getRetryAfterHeader() @ L64

  • 方法名:getRetryAfterHeader
  • 源码定位:L64
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public static int getRetryAfterHeader(HttpURLConnection connection) @ L68

  • 方法名:getRetryAfterHeader
  • 源码定位:L68
  • 返回类型:int
  • 修饰符:public static

参数:

  • connection: HttpURLConnection

说明:

TODO

public int responseCode() @ L78

  • 方法名:responseCode
  • 源码定位:L78
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public String text() @ L87

  • 方法名:text
  • 源码定位:L87
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

private String read(InputStream in) @ L104

  • 方法名:read
  • 源码定位:L104
  • 返回类型:String
  • 修饰符:private

参数:

  • in: InputStream

说明:

TODO

private void dispose() @ L119

  • 方法名:dispose
  • 源码定位:L119
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

protected T connect() @ L150

  • 方法名:connect
  • 源码定位:L150
  • 返回类型:T
  • 修饰符:protected

参数:

说明:

TODO

protected abstract T doConnect() @ L160

  • 方法名:doConnect
  • 源码定位:L160
  • 返回类型:T
  • 修饰符:protected abstract

参数:

说明:

TODO

public static Request<?> get(String url) @ L162

  • 方法名:get
  • 源码定位:L162
  • 返回类型:Request<?>
  • 修饰符:public static

参数:

  • url: String

说明:

TODO

public static Request<?> get(String url, int connectTimeoutMillis, int readTimeoutMillis) @ L166

  • 方法名:get
  • 源码定位:L166
  • 返回类型:Request<?>
  • 修饰符:public static

参数:

  • url: String
  • connectTimeoutMillis: int
  • readTimeoutMillis: int

说明:

TODO

public static Request<?> post(String uri, String content) @ L170

  • 方法名:post
  • 源码定位:L170
  • 返回类型:Request<?>
  • 修饰符:public static

参数:

  • uri: String
  • content: String

说明:

TODO

public static Request<?> post(String uri, String content, int connectTimeoutMillis, int readTimeoutMillis) @ L174

  • 方法名:post
  • 源码定位:L174
  • 返回类型:Request<?>
  • 修饰符:public static

参数:

  • uri: String
  • content: String
  • connectTimeoutMillis: int
  • readTimeoutMillis: int

说明:

TODO

public static Request<?> delete(String url) @ L178

  • 方法名:delete
  • 源码定位:L178
  • 返回类型:Request<?>
  • 修饰符:public static

参数:

  • url: String

说明:

TODO

public static Request<?> put(String url, String content) @ L182

  • 方法名:put
  • 源码定位:L182
  • 返回类型:Request<?>
  • 修饰符:public static

参数:

  • url: String
  • content: String

说明:

TODO

public static Request<?> put(String url, String content, int connectTimeoutMillis, int readTimeoutMillis) @ L186

  • 方法名:put
  • 源码定位:L186
  • 返回类型:Request<?>
  • 修饰符:public static

参数:

  • url: String
  • content: String
  • connectTimeoutMillis: int
  • readTimeoutMillis: int

说明:

TODO

public String getHeader(String header) @ L190

  • 方法名:getHeader
  • 源码定位:L190
  • 返回类型:String
  • 修饰符:public

参数:

  • header: String

说明:

TODO

public static String getHeader(HttpURLConnection connection, String header) @ L194

  • 方法名:getHeader
  • 源码定位:L194
  • 返回类型:String
  • 修饰符:public static

参数:

  • connection: HttpURLConnection
  • header: String

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public abstract class Request<T extends Request<T>> {
    protected HttpURLConnection connection;
    private boolean connected;
    protected String url;
    private static final int DEFAULT_READ_TIMEOUT = 60000;
    private static final int DEFAULT_CONNECT_TIMEOUT = 5000;
    private static final String IS_SNAPSHOT_KEY = "Is-Prerelease";
    private static final String COOKIE_KEY = "Cookie";
 
    public Request(String url, int connectTimeout, int readTimeout) {
        try {
            this.url = url;
            Proxy proxy = RealmsClientConfig.getProxy();
            if (proxy != null) {
                this.connection = (HttpURLConnection)new URL(url).openConnection(proxy);
            } else {
                this.connection = (HttpURLConnection)new URL(url).openConnection();
            }
 
            this.connection.setConnectTimeout(connectTimeout);
            this.connection.setReadTimeout(readTimeout);
        } catch (MalformedURLException var5) {
            throw new RealmsHttpException(var5.getMessage(), var5);
        } catch (IOException var6) {
            throw new RealmsHttpException(var6.getMessage(), var6);
        }
    }
 
    public void cookie(String key, String value) {
        cookie(this.connection, key, value);
    }
 
    public static void cookie(HttpURLConnection connection, String key, String value) {
        String cookie = connection.getRequestProperty("Cookie");
        if (cookie == null) {
            connection.setRequestProperty("Cookie", key + "=" + value);
        } else {
            connection.setRequestProperty("Cookie", cookie + ";" + key + "=" + value);
        }
    }
 
    public void addSnapshotHeader(boolean isSnapshot) {
        this.connection.addRequestProperty("Is-Prerelease", String.valueOf(isSnapshot));
    }
 
    public int getRetryAfterHeader() {
        return getRetryAfterHeader(this.connection);
    }
 
    public static int getRetryAfterHeader(HttpURLConnection connection) {
        String pauseTime = connection.getHeaderField("Retry-After");
 
        try {
            return Integer.valueOf(pauseTime);
        } catch (Exception var3) {
            return 5;
        }
    }
 
    public int responseCode() {
        try {
            this.connect();
            return this.connection.getResponseCode();
        } catch (Exception var2) {
            throw new RealmsHttpException(var2.getMessage(), var2);
        }
    }
 
    public String text() {
        try {
            this.connect();
            String result;
            if (this.responseCode() >= 400) {
                result = this.read(this.connection.getErrorStream());
            } else {
                result = this.read(this.connection.getInputStream());
            }
 
            this.dispose();
            return result;
        } catch (IOException var2) {
            throw new RealmsHttpException(var2.getMessage(), var2);
        }
    }
 
    private String read(@Nullable InputStream in) throws IOException {
        if (in == null) {
            return "";
        } else {
            InputStreamReader streamReader = new InputStreamReader(in, StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
 
            for (int x = streamReader.read(); x != -1; x = streamReader.read()) {
                sb.append((char)x);
            }
 
            return sb.toString();
        }
    }
 
    private void dispose() {
        byte[] bytes = new byte[1024];
 
        try {
            InputStream in = this.connection.getInputStream();
 
            while (in.read(bytes) > 0) {
            }
 
            in.close();
            return;
        } catch (Exception var9) {
            try {
                InputStream errorStream = this.connection.getErrorStream();
                if (errorStream != null) {
                    while (errorStream.read(bytes) > 0) {
                    }
 
                    errorStream.close();
                    return;
                }
            } catch (IOException var8) {
                return;
            }
        } finally {
            if (this.connection != null) {
                this.connection.disconnect();
            }
        }
    }
 
    protected T connect() {
        if (this.connected) {
            return (T)this;
        } else {
            T t = this.doConnect();
            this.connected = true;
            return t;
        }
    }
 
    protected abstract T doConnect();
 
    public static Request<?> get(String url) {
        return new Request.Get(url, 5000, 60000);
    }
 
    public static Request<?> get(String url, int connectTimeoutMillis, int readTimeoutMillis) {
        return new Request.Get(url, connectTimeoutMillis, readTimeoutMillis);
    }
 
    public static Request<?> post(String uri, String content) {
        return new Request.Post(uri, content, 5000, 60000);
    }
 
    public static Request<?> post(String uri, String content, int connectTimeoutMillis, int readTimeoutMillis) {
        return new Request.Post(uri, content, connectTimeoutMillis, readTimeoutMillis);
    }
 
    public static Request<?> delete(String url) {
        return new Request.Delete(url, 5000, 60000);
    }
 
    public static Request<?> put(String url, String content) {
        return new Request.Put(url, content, 5000, 60000);
    }
 
    public static Request<?> put(String url, String content, int connectTimeoutMillis, int readTimeoutMillis) {
        return new Request.Put(url, content, connectTimeoutMillis, readTimeoutMillis);
    }
 
    public String getHeader(String header) {
        return getHeader(this.connection, header);
    }
 
    public static String getHeader(HttpURLConnection connection, String header) {
        try {
            return connection.getHeaderField(header);
        } catch (Exception var3) {
            return "";
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class Delete extends Request<Request.Delete> {
        public Delete(String uri, int connectTimeout, int readTimeout) {
            super(uri, connectTimeout, readTimeout);
        }
 
        public Request.Delete doConnect() {
            try {
                this.connection.setDoOutput(true);
                this.connection.setRequestMethod("DELETE");
                this.connection.connect();
                return this;
            } catch (Exception var2) {
                throw new RealmsHttpException(var2.getMessage(), var2);
            }
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class Get extends Request<Request.Get> {
        public Get(String uri, int connectTimeout, int readTimeout) {
            super(uri, connectTimeout, readTimeout);
        }
 
        public Request.Get doConnect() {
            try {
                this.connection.setDoInput(true);
                this.connection.setDoOutput(true);
                this.connection.setUseCaches(false);
                this.connection.setRequestMethod("GET");
                return this;
            } catch (Exception var2) {
                throw new RealmsHttpException(var2.getMessage(), var2);
            }
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class Post extends Request<Request.Post> {
        private final String content;
 
        public Post(String uri, String content, int connectTimeout, int readTimeout) {
            super(uri, connectTimeout, readTimeout);
            this.content = content;
        }
 
        public Request.Post doConnect() {
            try {
                if (this.content != null) {
                    this.connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                }
 
                this.connection.setDoInput(true);
                this.connection.setDoOutput(true);
                this.connection.setUseCaches(false);
                this.connection.setRequestMethod("POST");
                OutputStream out = this.connection.getOutputStream();
                OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
                writer.write(this.content);
                writer.close();
                out.flush();
                return this;
            } catch (Exception var3) {
                throw new RealmsHttpException(var3.getMessage(), var3);
            }
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class Put extends Request<Request.Put> {
        private final String content;
 
        public Put(String uri, String content, int connectTimeout, int readTimeout) {
            super(uri, connectTimeout, readTimeout);
            this.content = content;
        }
 
        public Request.Put doConnect() {
            try {
                if (this.content != null) {
                    this.connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                }
 
                this.connection.setDoOutput(true);
                this.connection.setDoInput(true);
                this.connection.setRequestMethod("PUT");
                OutputStream out = this.connection.getOutputStream();
                OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
                writer.write(this.content);
                writer.close();
                out.flush();
                return this;
            } catch (Exception var3) {
                throw new RealmsHttpException(var3.getMessage(), var3);
            }
        }
    }
}

引用的其他类