diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index 4b073e20b..0f43aec00 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -334,57 +334,58 @@ private WebResourceResponse handleLocalRequest(WebResourceRequest request, PathH * @return */ private WebResourceResponse handleProxyRequest(WebResourceRequest request, PathHandler handler) { - final String method = request.getMethod(); - if (method.equals("GET")) { - try { - String url = request.getUrl().toString(); - Map headers = request.getRequestHeaders(); - boolean isHtmlText = false; - for (Map.Entry header : headers.entrySet()) { - if (header.getKey().equalsIgnoreCase("Accept") && header.getValue().toLowerCase().contains("text/html")) { - isHtmlText = true; - break; - } - } - if (isHtmlText) { - HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + if (jsInjector != null) { + final String method = request.getMethod(); + if (method.equals("GET")) { + try { + String url = request.getUrl().toString(); + Map headers = request.getRequestHeaders(); + boolean isHtmlText = false; for (Map.Entry header : headers.entrySet()) { - conn.setRequestProperty(header.getKey(), header.getValue()); - } - String getCookie = CookieManager.getInstance().getCookie(url); - if (getCookie != null) { - conn.setRequestProperty("Cookie", getCookie); - } - conn.setRequestMethod(method); - conn.setReadTimeout(30 * 1000); - conn.setConnectTimeout(30 * 1000); - if (request.getUrl().getUserInfo() != null) { - byte[] userInfoBytes = request.getUrl().getUserInfo().getBytes(StandardCharsets.UTF_8); - String base64 = Base64.encodeToString(userInfoBytes, Base64.NO_WRAP); - conn.setRequestProperty("Authorization", "Basic " + base64); + if (header.getKey().equalsIgnoreCase("Accept") && header.getValue().toLowerCase().contains("text/html")) { + isHtmlText = true; + break; + } } + if (isHtmlText) { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + for (Map.Entry header : headers.entrySet()) { + conn.setRequestProperty(header.getKey(), header.getValue()); + } + String getCookie = CookieManager.getInstance().getCookie(url); + if (getCookie != null) { + conn.setRequestProperty("Cookie", getCookie); + } + conn.setRequestMethod(method); + conn.setReadTimeout(30 * 1000); + conn.setConnectTimeout(30 * 1000); + if (request.getUrl().getUserInfo() != null) { + byte[] userInfoBytes = request.getUrl().getUserInfo().getBytes(StandardCharsets.UTF_8); + String base64 = Base64.encodeToString(userInfoBytes, Base64.NO_WRAP); + conn.setRequestProperty("Authorization", "Basic " + base64); + } - List cookies = conn.getHeaderFields().get("Set-Cookie"); - if (cookies != null) { - for (String cookie : cookies) { - CookieManager.getInstance().setCookie(url, cookie); + List cookies = conn.getHeaderFields().get("Set-Cookie"); + if (cookies != null) { + for (String cookie : cookies) { + CookieManager.getInstance().setCookie(url, cookie); + } } - } - InputStream responseStream = conn.getInputStream(); - if (jsInjector != null) { + InputStream responseStream = conn.getInputStream(); responseStream = jsInjector.getInjectedStream(responseStream); + + return new WebResourceResponse( + "text/html", + handler.getEncoding(), + handler.getStatusCode(), + handler.getReasonPhrase(), + handler.getResponseHeaders(), + responseStream + ); } - return new WebResourceResponse( - "text/html", - handler.getEncoding(), - handler.getStatusCode(), - handler.getReasonPhrase(), - handler.getResponseHeaders(), - responseStream - ); + } catch (Exception ex) { + bridge.handleAppUrlLoadError(ex); } - } catch (Exception ex) { - bridge.handleAppUrlLoadError(ex); } } return null;