Java HTTP Read getContent(String urlStr)

Here you can find the source of getContent(String urlStr)

Description

get Content

License

Open Source License

Declaration

public static String getContent(String urlStr) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String getContent(String urlStr) {
        StringBuilder sb = new StringBuilder();
        InputStreamReader inputStreamReader = null;
        BufferedReader br = null;
        try {/*w w  w  . ja  v a 2 s .  c o m*/
            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection();

            inputStreamReader = new InputStreamReader(conn.getInputStream());
            br = new BufferedReader(inputStreamReader);

            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            throw new RuntimeException("Url connection failed:" + urlStr, e);
        } finally {
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
        return sb.toString();
    }
}

Related

  1. getByteaArrayFromConn(HttpURLConnection conn, boolean isSuccess)
  2. getContent(final HttpURLConnection con)
  3. getContent(final String urlString)
  4. getContentAsString(URL url)
  5. getContentEncoding(URL url)
  6. getContentFromHttpGetBasicAuth(String urlString, String username, String password)
  7. getContentFromURL(String sURL)