From 2a68fa01dd77d45b0c459de71c621dd60dd2334d Mon Sep 17 00:00:00 2001 From: tom79 Date: Fri, 27 Mar 2020 18:31:46 +0100 Subject: [PATCH] Fix issue #415 --- .../android/client/HttpsConnection.java | 123 ++++++++++++------ 1 file changed, 84 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java index 142aa86d0..cd21db463 100644 --- a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java +++ b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java @@ -267,50 +267,95 @@ public class HttpsConnection { URL url = new URL(urlConnection); - - if (proxy != null) - httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); - else - httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setConnectTimeout(30 * 1000); - httpsURLConnection.setRequestProperty("http.keepAlive", "false"); - httpsURLConnection.setRequestProperty("Content-Type", "application/json"); - httpsURLConnection.setRequestProperty("Accept", "application/json"); - httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); - httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); - httpsURLConnection.setRequestMethod("GET"); - httpsURLConnection.setDefaultUseCaches(true); - httpsURLConnection.setUseCaches(true); - String response; - if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { - getSinceMaxId(); - response = converToString(httpsURLConnection.getInputStream()); - } else { - String error = null; - if (httpsURLConnection.getErrorStream() != null) { - InputStream stream = httpsURLConnection.getErrorStream(); - if (stream == null) { - stream = httpsURLConnection.getInputStream(); - } - try (Scanner scanner = new Scanner(stream)) { - scanner.useDelimiter("\\Z"); - if (scanner.hasNext()) { - error = scanner.next(); + if (urlConnection.startsWith("https://")) { + if (proxy != null) + httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy); + else + httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setConnectTimeout(30 * 1000); + httpsURLConnection.setRequestProperty("http.keepAlive", "false"); + httpsURLConnection.setRequestProperty("Content-Type", "application/json"); + httpsURLConnection.setRequestProperty("Accept", "application/json"); + httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance)); + httpsURLConnection.setRequestMethod("GET"); + httpsURLConnection.setDefaultUseCaches(true); + httpsURLConnection.setUseCaches(true); + String response; + if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { + getSinceMaxId(); + response = converToString(httpsURLConnection.getInputStream()); + } else { + String error = null; + if (httpsURLConnection.getErrorStream() != null) { + InputStream stream = httpsURLConnection.getErrorStream(); + if (stream == null) { + stream = httpsURLConnection.getInputStream(); + } + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); } - } catch (Exception e) { - e.printStackTrace(); } + int responseCode = httpsURLConnection.getResponseCode(); + try { + httpsURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); } - int responseCode = httpsURLConnection.getResponseCode(); - try { - httpsURLConnection.getInputStream().close(); - } catch (Exception ignored) { + getSinceMaxId(); + httpsURLConnection.getInputStream().close(); + return response; + }else{ + if (proxy != null) + httpURLConnection = (HttpURLConnection) url.openConnection(proxy); + else + httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setConnectTimeout(30 * 1000); + httpURLConnection.setRequestProperty("http.keepAlive", "false"); + httpURLConnection.setRequestProperty("Content-Type", "application/json"); + httpURLConnection.setRequestProperty("Accept", "application/json"); + httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setDefaultUseCaches(true); + httpURLConnection.setUseCaches(true); + String response; + if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { + getSinceMaxId(); + response = converToString(httpURLConnection.getInputStream()); + } else { + String error = null; + if (httpURLConnection.getErrorStream() != null) { + InputStream stream = httpURLConnection.getErrorStream(); + if (stream == null) { + stream = httpURLConnection.getInputStream(); + } + try (Scanner scanner = new Scanner(stream)) { + scanner.useDelimiter("\\Z"); + if (scanner.hasNext()) { + error = scanner.next(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + int responseCode = httpURLConnection.getResponseCode(); + try { + httpURLConnection.getInputStream().close(); + } catch (Exception ignored) { + } + throw new HttpsConnectionException(responseCode, error); } - throw new HttpsConnectionException(responseCode, error); + getSinceMaxId(); + httpURLConnection.getInputStream().close(); + return response; } - getSinceMaxId(); - httpsURLConnection.getInputStream().close(); - return response; + }