Java HTTP Read getContentFromURL(String sURL)

Here you can find the source of getContentFromURL(String sURL)

Description

get Content From URL

License

Open Source License

Declaration

public static String getContentFromURL(String sURL) 

Method Source Code

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

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

import java.util.Scanner;

public class Main {
    public static String getContentFromURL(String sURL) {
        String content = "ERROR";

        URL url = null;/*from ww w  .  j a  v  a 2 s.  c o m*/
        try {
            url = new URL(sURL);

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

            Scanner s;
            if (connection.getResponseCode() != 200) {
                s = new Scanner(connection.getErrorStream());
            } else {
                s = new Scanner(connection.getInputStream());
            }
            s.useDelimiter("\\Z");
            content = s.next();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return content;
    }
}

Related

  1. getContent(final String urlString)
  2. getContent(String urlStr)
  3. getContentAsString(URL url)
  4. getContentEncoding(URL url)
  5. getContentFromHttpGetBasicAuth(String urlString, String username, String password)
  6. getContentFromURL(String url)
  7. getContentLength(URL url)
  8. getContentLength(URLConnection con)
  9. getContents(String urlStr)