DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Java - GetHTMLpage

04.20.2006
| 3195 views |
  • submit to reddit
        // Scarica dalla rete una pagina HTML

package HttpGetIMGs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/*
 * 
 * Fa una richiesta di connessione al web e scarica la pagina...
 */

public class RandomIMGs
{
	private BufferedReader br;
	private OutputStreamWriter osw;
	private String data;
	private String line;
	private URL url;
	private URLConnection conn;
		
	public RandomIMGs()
	{
		try
		{
			url = new URL("http://flickr.com/photos");
			conn = url.openConnection();
			conn.setDoOutput(true);
			
			osw = new OutputStreamWriter(conn.getOutputStream());
			
			data = URLEncoder.encode("start", "utf-8") + "=" + URLEncoder.encode("1", "utf-8");
			osw.write(data);
			osw.flush();
			
			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			while((line = br.readLine()) != null)
			{
				System.out.println(line);
			}
			
			osw.close();
			br.close();
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		new RandomIMGs();
	}
}