From cbe07b9003617cd5fa51982ff37a4d6aa26dfa6b Mon Sep 17 00:00:00 2001 From: xfl03 Date: Sun, 3 Mar 2019 11:34:54 +0800 Subject: [PATCH] Fix http redirect to https --- .../utils/HttpRequestUtil.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Common/source/customskinloader/utils/HttpRequestUtil.java b/Common/source/customskinloader/utils/HttpRequestUtil.java index ffcada5..14ee795 100644 --- a/Common/source/customskinloader/utils/HttpRequestUtil.java +++ b/Common/source/customskinloader/utils/HttpRequestUtil.java @@ -74,6 +74,10 @@ public class HttpRequestUtil { public static final File CACHE_DIR=new File(CustomSkinLoader.DATA_DIR,"caches"); public static HttpResponce makeHttpRequest(HttpRequest request){ + return makeHttpRequest(request, 0); + } + + public static HttpResponce makeHttpRequest(HttpRequest request, int redirectTime){ try{ CustomSkinLoader.logger.debug("Try to request '"+request.url+(request.userAgent==null?"'.":"' with user agent '"+request.userAgent+"'.")); //Check Cache @@ -134,6 +138,23 @@ public class HttpRequestUtil { CustomSkinLoader.logger.debug("Failed to request (Response Code: "+c.getResponseCode()+")"); return responce; } + if (res == HttpURLConnection.HTTP_MOVED_PERM || res == HttpURLConnection.HTTP_MOVED_TEMP) { + //Redirect + if (redirectTime >= 4) { + CustomSkinLoader.logger.debug("Failed to request (Too many redirection)"); + return responce; + } + request.url = c.getHeaderField("Location");//Get redirecting location + if (request.url == null) { + request.url = c.getHeaderField("location");//Another way + } + if (request.url == null) { + CustomSkinLoader.logger.debug("Failed to request (Redirecting location not found)"); + return responce; + } + CustomSkinLoader.logger.debug("Redirect to " + request.url); + return makeHttpRequest(request, redirectTime + 1);//Recursion + } responce.success=true; CustomSkinLoader.logger.debug("Successfully request (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")"); if(responce.responceCode==HttpURLConnection.HTTP_NOT_MODIFIED)