Java Utililty Methods URL Read

List of utility methods to do URL Read

Description

The list of methods to do URL Read are organized into topic(s).

Method

InputStreamgetStreamByConnection(final HttpURLConnection con)
Returns the inputstream or errorstream via the http-connection
InputStream stream = null;
int responseCode = con.getResponseCode();
if (responseCode > 399) {
    stream = con.getErrorStream();
} else {
    stream = con.getInputStream();
return stream;
...
StringgetStringFromConnection(HttpURLConnection connection)
get String From Connection
InputStream inputStream = null;
DataInputStream dataInputStream = null;
String lineData, completeData = "";
try {
    boolean error = false;
    if (connection.getResponseCode() >= 400) {
        inputStream = connection.getErrorStream();
        error = true;
...
StringgetStringFromURL(String URLString)
get String From URL
try {
    URL URL = new URL(URLString);
    HttpURLConnection connection = (HttpURLConnection) URL.openConnection();
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
    connection.setDoInput(true);
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    return reader.readLine();
...
StringBuffergetText(HttpURLConnection conn)
get Text
try {
    int rc = conn.getResponseCode();
    String s = "";
    StringBuffer html = new StringBuffer();
    try {
        BufferedReader inp = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((s = inp.readLine()) != null) {
            s = s.replaceAll("http://istgeo.ist.supsi.ch:80/basemaps/wms/", "ciao");
...
String[]getText(String url)
Returns the response from the given URL as an array of lines.
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
ArrayList<String> lines = new ArrayList<String>();
String inputLine;
while ((inputLine = in.readLine()) != null)
    lines.add(inputLine);
in.close();
...
StringgetText(String url)
get Text
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "iso-8859-2"));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
    response.append(inputLine);
in.close();
...
StringgetText(URL url)
get Text
final StringBuffer buffer = new StringBuffer();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept",
        "text/html,application/xhtml+xml,application/xml;" + "q=0.9,image/webp,*/*;q=0.8");
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = reader.readLine();
while (line != null) {
    buffer.append(line);
...
StringgetTextFromURL(final String url)
get Text From URL
StringBuilder result = new StringBuilder();
try {
    final URL website = new URL(url);
    final URLConnection connection = website.openConnection();
    final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        result.append(inputLine);
...
StringgetTextFromURL(URL url)
Reads the content of a URL as text using the default connect and read timeouts.
return getTextFromURL(url, defaultConnectTimeout(), defaultReadTimeout());
StringgetTextFromURL(URL url)
Reads the content of a URL as text using the default connect and read timeouts.
return getTextFromURL(url, defaultConnectTimeout(), defaultReadTimeout());