From ba9847a0df2a6ca7c1bb570ae8d808f2623687e7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 6 Oct 2020 14:07:48 -0700 Subject: Support URL redirection --- .../java/org/traccar/manager/StartFragment.java | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/traccar/manager/StartFragment.java b/app/src/main/java/org/traccar/manager/StartFragment.java index c7d5838..fd0849a 100644 --- a/app/src/main/java/org/traccar/manager/StartFragment.java +++ b/app/src/main/java/org/traccar/manager/StartFragment.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,8 @@ public class StartFragment extends Fragment implements View.OnClickListener { private static final String TAG = StartFragment.class.getSimpleName(); + private static int MAX_REDIRECTS = 5; + private EditText serverField; private Button startButton; @@ -67,8 +69,24 @@ public class StartFragment extends Fragment implements View.OnClickListener { try { Uri uri = Uri.parse(urls[0]).buildUpon().appendEncodedPath("api/server").build(); - URL url = new URL(uri.toString()); - HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + String url = uri.toString(); + HttpURLConnection urlConnection = null; + + for (int i = 0; i < MAX_REDIRECTS; i++) { + URL resourceUrl = new URL(url); + urlConnection = (HttpURLConnection) resourceUrl.openConnection(); + urlConnection.setInstanceFollowRedirects(false); + + switch (urlConnection.getResponseCode()) { + case HttpURLConnection.HTTP_MOVED_PERM: + case HttpURLConnection.HTTP_MOVED_TEMP: + url = urlConnection.getHeaderField("Location"); + continue; + } + + break; + } + BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line; -- cgit v1.2.3