+ // `HttpUrlConnection` sets `Accept-Encoding` to be `gzip` by default. If the property is manually set, than `HttpUrlConnection` does not process the decoding.
+ // Add the `Accept-Encoding` header to the string builder and format the text.
+ requestHeadersBuilder.append(System.getProperty("line.separator"));
+ if (Build.VERSION.SDK_INT >= 21) { // Newer versions of Android are so smart.
+ requestHeadersBuilder.append("Accept-Encoding", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+ } else { // Older versions not so much.
+ oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
+ requestHeadersBuilder.append("Accept-Encoding");
+ newRequestHeadersBuilderLength = requestHeadersBuilder.length();
+ requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+ }
+ requestHeadersBuilder.append(": gzip");
+
+ // Ignore SSL errors if requested.
+ if (ignoreSslErrors){
+ // Create a new host name verifier.
+ HostnameVerifier hostnameVerifier = (hostname, sslSession) -> {
+ // Allow all host names.
+ return true;
+ };
+
+ // Create a new trust manager.
+ TrustManager[] trustManager = new TrustManager[] {
+ new X509TrustManager() {
+ @SuppressLint("TrustAllX509TrustManager")
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType) {
+ // Do nothing, which trusts all client certificates.
+ }
+
+ @SuppressLint("TrustAllX509TrustManager")
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType) {
+ // Do nothing, which trusts all server certificates.
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+ }
+ };
+
+ // Get an SSL context. `TLS` provides a base instance available from API 1. <https://developer.android.com/reference/javax/net/ssl/SSLContext>
+ SSLContext sslContext = SSLContext.getInstance("TLS");
+
+ // Initialize the SSL context with the blank trust manager.
+ sslContext.init(null, trustManager, new SecureRandom());
+
+ // Get the SSL socket factory with the blank trust manager.
+ SSLSocketFactory socketFactory = sslContext.getSocketFactory();
+
+ // Set the HTTPS URL Connection to use the blank host name verifier.
+ ((HttpsURLConnection) httpUrlConnection).setHostnameVerifier(hostnameVerifier);
+
+ // Set the HTTPS URL connection to use the socket factory with the blank trust manager.
+ ((HttpsURLConnection) httpUrlConnection).setSSLSocketFactory(socketFactory);