Example usage for java.net URL openStream

List of usage examples for java.net URL openStream

Introduction

In this page you can find the example usage for java.net URL openStream.

Prototype

public final InputStream openStream() throws java.io.IOException 

Source Link

Document

Opens a connection to this URL and returns an InputStream for reading from that connection.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL myURL = new URL("http://www.google.com");
    BufferedReader so = new BufferedReader(new InputStreamReader(myURL.openStream()));
    while (true) {
        String output = so.readLine();
        if (output != null) {
            System.out.println(output);
        } else {/*from w  ww . j ava 2 s. c  o  m*/
            break;
        }
    }
    so.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

    URL url = new URL("https://www.verisign.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

    String line;/*  www .  j a v a  2  s  . co  m*/
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    in.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://hostname:80/index.html");

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;//w w w  .  j ava2 s  .  c o m
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
    in.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    URL url = new URL("http://www.java2s.com/style/download.png");

    InputStream inputStream = url.openStream();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    int n = 0;/*from w w  w  .  ja  v a2  s .co  m*/
    while (-1 != (n = inputStream.read(buffer))) {
        output.write(buffer, 0, n);
    }
    inputStream.close();

    byte[] data = output.toByteArray();

    OutputStream out = new FileOutputStream("data.png");
    out.write(data);
    out.close();

    for (byte b : data) {
        System.out.printf("0x%x ", b);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    try {//from w ww.  ja  v  a 2s. c o m

        URL url = new URL("http://www.java2s.com");

        // Obtain output stream
        InputStream is = url.openStream();

        // Read and display data from url
        byte buffer[] = new byte[1024];
        int i;
        while ((i = is.read(buffer)) != -1) {
            System.out.write(buffer, 0, i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.google.com");

    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter writer = new BufferedWriter(new FileWriter("data.html"));

    String line;/*  ww w  . j a  v  a  2  s .c  om*/
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        writer.write(line);
        writer.newLine();
    }

    reader.close();
    writer.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Authenticator.setDefault(new MyAuthenticator());
    URL url = new URL("http://hostname:80/index.html");

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;//ww  w. j a  v a2 s.  c o m
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
    in.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> names = new ArrayList<>();
    URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265");
    InputStream is = oracle.openStream();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);/*from   w  w  w .  j av a  2  s . com*/
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(is);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//*:*/@*");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nl = (NodeList) result;
    for (int i = 0; i < nl.getLength(); i++) {
        names.add(nl.item(i).getNodeName());
        Node node = nl.item(i);
        String path = "." + node.getNodeName() + " = " + node.getNodeValue();
        node = ((Attr) node).getOwnerElement();
        while (node != null) {
            path = node.getNodeName() + '/' + path;
            node = node.getParentNode();
        }
        System.out.println(path);
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(443);
    while (true) {
        Socket s = ss.accept();/*from   www .ja v a 2s  .  co m*/
        PrintStream out = new PrintStream(s.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String info = null;
        String request = null;
        String refer = null;

        while ((info = in.readLine()) != null) {
            if (info.startsWith("GET")) {
                request = info;
            }
            if (info.startsWith("Referer:")) {
                refer = info;
            }
            if (info.equals(""))
                break;
        }
        if (request != null) {
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            int sp1 = request.indexOf(' ');
            int sp2 = request.indexOf(' ', sp1 + 1);
            String filename = request.substring(sp1 + 2, sp2);
            if (refer != null) {
                sp1 = refer.indexOf(' ');
                refer = refer.substring(sp1 + 1, refer.length());
                if (!refer.endsWith("/")) {
                    refer = refer + "/";
                }
                filename = refer + filename;
            }
            URL con = new URL(filename);
            InputStream gotoin = con.openStream();
            int n = gotoin.available();
            byte buf[] = new byte[1024];
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            out.println("Content_Length:" + n + "\n");
            while ((n = gotoin.read(buf)) >= 0) {
                out.write(buf, 0, n);
            }
            out.close();
            s.close();
            in.close();
        }
    }
}

From source file:URLReader.java

public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream()));

    String inputLine;//from  www .  ja  va2 s.  c o m

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

    in.close();
}