# Http/Https Geschwindigkeit



## jb007 (28. November 2007)

Hallo,

ich habe mal eine Frage zur Performance von Http/Https Verbindungen.

In unserer Applikation gibt es zeitlich gesehen große Unterschiede zwischen beiden Protokollen.

Ein Http Request ist in der Regel zehnmal so schnell wie ein Https Request.

Wenn wir lokal in unserem Firmen Netzwerk arbeiten ist das nicht soooo schlimm, aber sobald wir von Extern arbeiten dauert ein Request statt 1 Sekunde ca. 5 Sekunden.

Rufe ich eine URL über den Browser auf (intern oder extern), dann geht das fast viermal so schnell als über unsere Applikation. Ein Unterschied zwischen Http/Https ist dabei kaum zu bemerken.

Ich frage mich jetzt ob das normal ist oder eventuell ein Fehler in unserer Serverkommunikation liegt 

Hier mal unser Client:


```
private static URLConnection buildHttpsConnection()
    {
        HttpsURLConnection connection = null;
        
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager()
            {
                public java.security.cert.X509Certificate[] getAcceptedIssuers()
                {
                    return null;
                }

                public void checkClientTrusted(
                    java.security.cert.X509Certificate[] certs, String authType)
                {
                }

                public void checkServerTrusted(
                    java.security.cert.X509Certificate[] certs, String authType)
                {
                }
            }
        };
        
        SSLContext sc = null;

        // Install the all-trusting trust manager
        try
        {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            SSLContext.setDefault(sc);
            
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        }
        catch (Exception e)
        {
            System.out.println("### exception when disabling trust authentication ###");
            e.printStackTrace();
        }
        
        try
        {
            URL url;

            //SSLSocketFactory ssf = sc.getSocketFactory();
            //HttpsURLConnection.setDefaultSSLSocketFactory(ssf);

            HostnameVerifier hv = new HostnameVerifier()
            {
                public boolean verify(String urlHostName, SSLSession session)
                {
                    System.out.println("### Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
                    return true;
                }
            };

            HttpsURLConnection.setDefaultHostnameVerifier(hv);

            url = new URL(getServerUrl());

            // URL connection channel.
            connection = (HttpsURLConnection) url.openConnection();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }

        return connection;
    }



    public static URLConnection buildHttpConnection()
    {
        URLConnection connection = null;

        URL url;

        try
        {
            url = new URL(getServerUrl());
            connection = url.openConnection();
        }
        catch (MalformedURLException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }

        return connection;
    }



    public static String requestServer()
    {
        Date startTime   = new Date();
        Date currentTime = new Date();
        long curTime      = 0;
        
        String responseString = "";

        URLConnection connection = null;

        //build a connection handler depending on url
        if (getServerUrl().startsWith("https"))
        {
            connection = buildHttpsConnection();
        }
        else
        {
            connection = buildHttpConnection();
        }

        if (connection == null)
        {
            System.out.println("# Connection is null");
            return responseString;
        }

        //
        try
        {
            URL url = new URL(getServerUrl());

            //URLConnection       urlConn;
            DataOutputStream printout;
            DataInputStream input;

            // Let the run-time system (RTS) know that we want input.
            connection.setDoInput(true);
            // Let the RTS know that we want to do output.
            connection.setDoOutput(true);
            // No caching, we want the real thing.
            connection.setUseCaches(false);
            // Specify the content type.
            connection.setRequestProperty("Content-Type", getContentType());
            connection.setRequestProperty("Connection", "close");

            if("true".equals(getAcceptEncoding()))
            {
                System.out.println("# Set encoding");
                connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
            }
            
            if (url.getUserInfo() != null)
            {
                String hostName = url.getHost();

                String authUser = url.getUserInfo().substring(0, url.getUserInfo().lastIndexOf(":"));
                String authPass = url.getUserInfo().substring(url.getUserInfo().lastIndexOf(":") + 1);

                //encode base 64
                String userAuth = authUser + ":" + authPass;
                connection.setRequestProperty("Authorization", "Basic " + Base64.encode(userAuth));
            }

            try
            {
                currentTime = new Date();
                curTime = currentTime.getTime() - startTime.getTime();
                System.out.println("# Verbindung wurde konfiguriert!");
                System.out.println("# Beginne Daten zum Server zu senden : " + curTime);                
                
                // Send POST output.
                printout = new DataOutputStream(connection.getOutputStream());

                printout.writeBytes(getRequestString());
                printout.flush();
                printout.close();

                currentTime = new Date();
                curTime = currentTime.getTime() - startTime.getTime();
                System.out.println("# Daten wurden zum Server gesendet : " + curTime);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                return "";
            }

            //obtain the encoding returned by the server
            String encoding = connection.getContentEncoding();

            InputStream inputStream = null;

            //check encoding
            if (encoding != null && encoding.equalsIgnoreCase("gzip"))
            {
                inputStream = new GZIPInputStream(connection.getInputStream());
            }
            else if (encoding != null && encoding.equalsIgnoreCase("deflate"))
            {
                inputStream = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
            }
            else
            {
                inputStream = connection.getInputStream();
            }

            input = new DataInputStream(inputStream);

            String str;

            BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));

            StringBuffer buffer = new StringBuffer();

            currentTime = new Date();
            curTime = currentTime.getTime() - startTime.getTime();
            System.out.println("# Server sendet Antwort: " + curTime);

            while (null != ((str = inputReader.readLine())))
            {
                buffer.append(str);
            }

            currentTime = new Date();
            curTime = currentTime.getTime() - startTime.getTime();
            System.out.println("# Antwort erhalten: " + curTime);

            input.close();

            return buffer.toString();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "";
        }
    }
```

Fällt hier vielleicht Jemandem etwas auf was die Performance beinflußt ?

Vielen Dank schon einmal für eure Antworten.

Gruß Jens


----------

