Accessing the Contents of a URL - Java Network

Java examples for Network:URL

Description

Accessing the Contents of a URL

Demo Code

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class Main {
  public static void main(String[] args) throws Exception {
    String urlStr = "https://java2s.com";
    BufferedReader br = null;//from  w w  w . ja  v a  2  s . co  m
    URL url = new URL(urlStr);
    InputStream ins = url.openStream();
    br = new BufferedReader(new InputStreamReader(ins));
    StringBuilder sb = new StringBuilder();
    String msg = null;
    while ((msg = br.readLine()) != null) {
      sb.append(msg);
      sb.append("\n");
    }

    System.out.println(sb.toString());
  }
}

Result


Related Tutorials