title: Increase performance of a HttpsURLConnection, getInputStream() very slow
body: <p>This is basically how I am reading the content from an URL and get the content in the variable <code>result</code>. The result is a json parsed after.</p>
  
  <p>I was wondering if it was a way to increase the performance of this request as it takes up to 4~5 seconds while I would like it to take, if possible, less than 2.</p>
  
  <p>Thing is <code>trustAllHosts()</code> and <code>BufferedReader br = new BufferedReader(new InputStreamReader(in));</code> seems to take some time, but I'm not sure about that. Any idea will be appreciated!</p>
  
  <pre><code>try {
          URL url;
          // get URL content
          url = new URL(apiURL);
          trustAllHosts();
          conn = (HttpsURLConnection) url.openConnection();
          conn.setHostnameVerifier(DO_NOT_VERIFY);
          conn.setRequestMethod("GET");
          conn.setRequestProperty("some values here");
          conn.setConnectTimeout(10000);
  
          in=conn.getInputStream();
  
          // open the stream and put it into BufferedReader
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
  
          while ((line=br.readLine())!= null) {
              builder.append(line);
          }
  
          result=builder.toString();
          //System.out.print(result);
          br.close();
  
  
      } catch (MalformedURLException e) {
          result=null;
      } catch (java.net.SocketTimeoutException e) {
          result=null;
      } catch (IOException e) {
          result=null;
      }
      catch (Exception e) {
          result=null;
      }
      finally {
          try {
              in.close();
          }catch(Exception e){}
          try {
              conn.disconnect();
          }catch(Exception e){}
  
          return result;
      }
  </code></pre>
  
  <p><code>trustAllHosts()</code> :</p>
  
  <pre><code>/**
       * Trust every server - dont check for any certificate
       */
      private static void trustAllHosts() {
          // Create a trust manager that does not validate certificate chains
          TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                  return new java.security.cert.X509Certificate[] {};
              }
  
              public void checkClientTrusted(X509Certificate[] chain,
                                             String authType) throws CertificateException {
              }
  
              public void checkServerTrusted(X509Certificate[] chain,
                                             String authType) throws CertificateException {
              }
          } };
  
          // Install the all-trusting trust manager
          try {
              SSLContext sc = SSLContext.getInstance("TLS");
              sc.init(null, trustAllCerts, new java.security.SecureRandom());
              HttpsURLConnection
                      .setDefaultSSLSocketFactory(sc.getSocketFactory());
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>