Java URL Read getStringFromConnection(HttpURLConnection connection)

Here you can find the source of getStringFromConnection(HttpURLConnection connection)

Description

get String From Connection

License

Open Source License

Declaration

public static String getStringFromConnection(HttpURLConnection connection) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;

import java.io.BufferedReader;
import java.io.DataInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

public class Main {
    public static String getStringFromConnection(HttpURLConnection connection) {
        InputStream inputStream = null;
        DataInputStream dataInputStream = null;
        String lineData, completeData = "";

        try {// w  ww.j a  va  2s  .co m
            boolean error = false;
            if (connection.getResponseCode() >= 400) {
                inputStream = connection.getErrorStream();
                error = true;
            } else {
                inputStream = connection.getInputStream();
            }
            dataInputStream = new DataInputStream(new BufferedInputStream(inputStream));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));

            while ((lineData = bufferedReader.readLine()) != null) {
                if (lineData.startsWith("ERROR")) {
                    inputStream.close();
                    dataInputStream.close();
                    bufferedReader.close();
                    return lineData;
                }
                completeData += lineData;
            }

            if (error)
                System.out.println("Error Connecting to AutoUpdater. Response Follows: \n" + completeData);

            inputStream.close();
            bufferedReader.close();
            dataInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return completeData;
    }
}

Related

  1. getLastModified(HttpURLConnection conn)
  2. getLastModified(HttpURLConnection connection)
  3. getRemoteFileLength(String url)
  4. getRemoteFileSize(URL url)
  5. getStreamByConnection(final HttpURLConnection con)
  6. getStringFromURL(String URLString)
  7. getText(HttpURLConnection conn)
  8. getText(String url)
  9. getText(String url)