From d4bdcc6d090725295f2412dc7c87884f551d58aa Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 22 Aug 2017 18:26:28 +0200 Subject: [PATCH] Fixed url redirect --- .idea/misc.xml | 3 -- .../ericsson/uecontrol/core/util/UrlUtil.java | 28 +++++++++++++------ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index aa6abdb..f7ec151 100755 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,5 @@ - - - diff --git a/app/src/main/java/com/ericsson/uecontrol/core/util/UrlUtil.java b/app/src/main/java/com/ericsson/uecontrol/core/util/UrlUtil.java index be8ffad..f156474 100755 --- a/app/src/main/java/com/ericsson/uecontrol/core/util/UrlUtil.java +++ b/app/src/main/java/com/ericsson/uecontrol/core/util/UrlUtil.java @@ -4,7 +4,6 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; -import android.os.Handler; import com.ericsson.uecontrol.gui.MainActivity; import org.apache.log4j.Logger; @@ -12,6 +11,7 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import java.net.URLDecoder; /** * Created by ezivkoc on 2014-09-18. @@ -23,18 +23,30 @@ public class UrlUtil { if(!isNetworkAvailable()) throw new IOException("No Data Network Available"); - final URLConnection connection = url.openConnection(); + URLConnection connection = url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(3000); connection.setUseCaches(false); - /*new Thread(new Runnable() { - public void run() { - try{Thread.sleep(5000);}catch(Exception e){}; - log.error("Force disconnect URLConnection"); - connection. + // Handle redirects + if (connection instanceof HttpURLConnection) { + HttpURLConnection httpConn = (HttpURLConnection) connection; + httpConn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections + + switch (httpConn.getResponseCode()) + { + case HttpURLConnection.HTTP_MOVED_PERM: + case HttpURLConnection.HTTP_MOVED_TEMP: + String redirectSrt = URLDecoder.decode( + httpConn.getHeaderField("Location"), "UTF-8"); + URL redirectURL = new URL(url, redirectSrt); // Deal with relative URLs + log.info("HTTP Redirect: From: "+url+", to: "+redirectURL); + httpConn.disconnect(); + connection = getURLConnection(redirectURL); } - }).start();*/ + + } + connection.connect(); return connection;