Java URL Read getText(URL url)

Here you can find the source of getText(URL url)

Description

get Text

License

LGPL

Declaration

public static String getText(URL url) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String getText(URL url) throws IOException {
        final StringBuffer buffer = new StringBuffer();

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        //avoid Resteasy bug
        connection.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;" + "q=0.9,image/webp,*/*;q=0.8");

        //read response
        final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String line = reader.readLine();
        while (line != null) {
            buffer.append(line);/*from  www .  ja  va  2  s .  co m*/
            line = reader.readLine();
        }

        reader.close();

        return buffer.toString();
    }
}

Related

  1. getStringFromConnection(HttpURLConnection connection)
  2. getStringFromURL(String URLString)
  3. getText(HttpURLConnection conn)
  4. getText(String url)
  5. getText(String url)
  6. getTextFromURL(final String url)
  7. getTextFromURL(URL url)
  8. getTextFromURL(URL url)
  9. getURL(final URL url)