Use BufferedReader to read content from a URL : HttpURLConnection « Network Protocol « Java






Use BufferedReader to read content from a URL

    

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

public class Main {
  public static void main(String[] argv) throws Exception {
    URL url = new URL("http://www.java.com");
    URLConnection urlConnection = url.openConnection();
    HttpURLConnection connection = null;
    if (urlConnection instanceof HttpURLConnection) {
      connection = (HttpURLConnection) urlConnection;
    } else {
      System.out.println("Please enter an HTTP URL.");
      return;
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String urlString = "";
    String current;
    while ((current = in.readLine()) != null) {
      urlString += current;
    }
    System.out.println(urlString);
  }
}

   
    
    
    
  








Related examples in the same category

1.Get the date of a url connection
2.Get the document expiration date
3.Get the document Last-modified date
4.Show the content type
5.Show the content length
6.Display request method
7.Get response code
8.Display response message
9.Display header information
10.Download and display the content
11.A Web Page Source Viewer
12.Reading from a URLConnection
13.Check if a page exists
14.Identify ourself to a proxy
15.Connect through a Proxy
16.Preventing Automatic Redirects in a HTTP Connection
17.Converting x-www-form-urlencoded Data
18.Getting the Cookies from an HTTP Connection
19.Sending a Cookie to an HTTP Server
20.Getting the Response Headers from an HTTP Connection
21.java.net.Authenticator can be used to send the credentials when needed
22.Grabbing a page using socket
23.Http Header Helper
24.Http connection Utilities
25.Available Port Finder