diff --git a/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java b/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java index 7cc5956..f626d0a 100644 --- a/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java +++ b/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java @@ -1,6 +1,7 @@ package boxresin.library.javahttp; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; @@ -217,8 +218,7 @@ public void clearHeaders() * Send HTTP request to a web server. * * @throws SocketTimeoutException Occurs when timeout. - * @return An HTML response from the web server. It will return null if 'cancel' method is - * called during request. + * @return An HTML response from the web server. */ @NotNull public HttpResponse request() throws SocketTimeoutException, IOException @@ -277,4 +277,25 @@ public HttpResponse request() throws SocketTimeoutException, IOException connection.disconnect(); return new HttpResponse(statusCode, statusMessage, bufferStream, connection); } + + /** + * Send HTTP request to a web server. + * + * @param autoRedirection If it set to true, detect and re-request new URL automatically. + * @throws SocketTimeoutException Occurs when timeout. + * @return An HTML response from the web server. + */ + @NotNull + public HttpResponse request(boolean autoRedirection) throws SocketTimeoutException, IOException + { + HttpResponse response = request(); + @Nullable String newUrl = response.getHeader("Location"); + + if (autoRedirection && newUrl != null) + { + setUrl(newUrl); + return request(true); + } + else return response; + } }