Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static String getAsStringFromURL(String urlString) {
        String retval = "";
        InputStream is = null;
        if ((is = downloadFromURL(urlString)) != null) {
            try {
                retval = new BufferedReader(new InputStreamReader(is)).readLine();//String(IOUtils.toByteArray(is));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return retval;

    }

    public static InputStream downloadFromURL(String urlString) {
        InputStream retval = null;
        try {
            URL url = new URL(urlString);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //URLConnection ucon = url.openConnection();
            con.setRequestMethod("GET");
            con.setDoInput(true);
            retval = con.getInputStream();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return retval;
    }
}