HarshitVaish
10/1/2016 - 7:21 AM

HTTP Url Connection

HTTP Url Connection

            // Build the URL - STEP 1
            URL urlObject = new URL(url);

            // Open connection with the server - STEP 2
            HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();

            // Open an input stream - STEP 3
            InputStream inputStream = connection.getInputStream();

            // Create an input stream reader - STEP 4
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

            // Create a buffered reader to read the data more efficiently - STEP 5
            BufferedReader reader = new BufferedReader(inputStreamReader);

            // Read through the buffered reader and create string builder - STEP 6
            String line = reader.readLine();
            StringBuffer stringBuffer = new StringBuffer();

            while (line != null){
                stringBuffer.append(line);
                line = reader.readLine();
            }
            
            // Disconnect - STEP 7
            connection.disconnect();