add response callback
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.io.IO;
|
||||
import com.imyeyu.java.bean.CallbackArg;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
@@ -59,6 +60,12 @@ public class FileRequest extends JacksonRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest responseCallback(CallbackArg<HttpResponse> callback) {
|
||||
super.responseCallback(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileRequest body(Object object) {
|
||||
super.body(object);
|
||||
@@ -84,15 +91,16 @@ public class FileRequest extends JacksonRequest {
|
||||
}
|
||||
|
||||
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());
|
||||
return handleResponse(response -> {
|
||||
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());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user