diff --git a/src/main/java/com/imyeyu/network/ProgressiveRequest.java b/src/main/java/com/imyeyu/network/ProgressiveRequest.java index dfb37da..0417104 100644 --- a/src/main/java/com/imyeyu/network/ProgressiveRequest.java +++ b/src/main/java/com/imyeyu/network/ProgressiveRequest.java @@ -4,10 +4,7 @@ import com.imyeyu.io.IO; import com.imyeyu.java.TimiJava; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.client5.http.fluent.Response; -import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; -import org.apache.hc.core5.http.HttpResponse; -import org.apache.hc.core5.http.ProtocolException; import org.apache.hc.core5.http.io.HttpClientResponseHandler; import org.apache.hc.core5.http.io.entity.EntityUtils; @@ -24,24 +21,27 @@ import java.nio.file.Path; * @author 夜雨 * @since 2025-06-26 13:03 */ -public class ProgressiveRequest { +public class ProgressiveRequest extends FileRequest { - private final Request request; - private final ProgressiveCallback callback; + protected final ProgressiveCallback callback; - private ProgressiveRequest(Request request, ProgressiveCallback callback) { - this.request = request; + protected ProgressiveRequest(Request request, ProgressiveCallback callback) { + super(request); this.callback = callback; } - public static ProgressiveRequest wrap(Request request) { - return new ProgressiveRequest(request, null); - } - public static ProgressiveRequest wrap(Request request, ProgressiveCallback callback) { return new ProgressiveRequest(request, callback); } + public static ProgressiveRequest get(String url, ProgressiveCallback callback) { + return new ProgressiveRequest(Request.get(url), callback); + } + + public static ProgressiveRequest post(String url, ProgressiveCallback callback) { + return new ProgressiveRequest(Request.post(url), callback); + } + public void toFile(Path outputPath) throws IOException, NoPermissionException { processResponse(request.execute(), IO.getOutputStream(outputPath.toFile())); } @@ -50,25 +50,17 @@ public class ProgressiveRequest { processResponse(request.execute(), IO.getOutputStream(file)); } + @Override + public String asString() throws IOException { + return new String(asBytes()); + } + public byte[] asBytes() throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); processResponse(request.execute(), os); return os.toByteArray(); } - public long length() throws IOException, ProtocolException { - HttpResponse response = request.execute().returnResponse(); - final int status = response.getCode(); - if (status < 200 || 300 <= status) { - throw new IOException("HTTP error status: " + status); - } - final Header contentLengthHeader = response.getHeader("Content-Length"); - if (contentLengthHeader == null) { - throw new IOException("Content-Length header missing"); - } - return Long.parseLong(contentLengthHeader.getValue()); - } - private void processResponse(Response response, OutputStream os) throws IOException { response.handleResponse((HttpClientResponseHandler) resp -> { HttpEntity entity = resp.getEntity();