Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:java:java_web

< Java

Java web

Webtartalom lekérése

Program01.java
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
 
class Program01
{
	public static void main(String args[])
	{
		InputStreamReader stream = null;
		BufferedReader reader = null;
		try 
		{
			URL url = new URL("http://szit.hu");
			URLConnection con = url.openConnection();
			stream = new InputStreamReader(con.getInputStream());
			reader = new BufferedReader(stream);
			System.out.println(reader.readLine());
			System.out.println(reader.readLine());
		}catch(MalformedURLException e)
		{
			System.out.println("Hibás URL");	
		}catch(IOException e)
		{
			System.out.println("Hiba a kapcsolatban");
		}finally
		{
			try{
				reader.close();
				stream.close();
			}catch(IOException e){}
		}
 
	}
}

Kivétel szétválasztással

Program01.java
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
 
class Program01 {
 
	InputStreamReader stream = null;
	BufferedReader reader = null;	
 
	public void getWebpage() {
		try {
			tryGetWebpage();
		}catch(MalformedURLException e)	{
			System.out.println("Hibás URL");	
		}catch(IOException e) {
			System.out.println("Hiba a kapcsolatban");
		}finally {
			try{
				reader.close();
				stream.close();
			}catch(IOException e){}
		}
 
 
	}
 
	public void tryGetWebpage() throws MalformedURLException,
			IOException {
 
		URL url = new URL("http://szit.hu");
		URLConnection con = url.openConnection();
		stream = new InputStreamReader(con.getInputStream());
		reader = new BufferedReader(stream);
 
		System.out.println(reader.readLine());
		System.out.println(reader.readLine());
 
	}
 
	public static void main(String args[]) 	{
		Program01 program01 = new Program01();
		program01.getWebpage();
	}
}

Egész weblap beolvasása

For ciklussal:

for (String line = reader.readLine(); line != null; line = reader.readLine()) {
	System.out.println(line);
}

While ciklussal:

String line = null;
while ( (line = reader.readLine()) != null ) {
	System.out.println(line);
}
Program01.java
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
 
class Program01 {
 
	InputStreamReader stream = null;
	BufferedReader reader = null;	
 
	public void getWebpage() {
		try {
			tryGetWebpage();
		}catch(MalformedURLException e)	{
			System.out.println("Hibás URL");	
		}catch(IOException e) {
			System.out.println("Hiba a kapcsolatban");
		}finally {
			try{
				reader.close();
				stream.close();
			}catch(IOException e){}
		}
 
 
	}
 
	public void tryGetWebpage() throws MalformedURLException,
			IOException {
 
		URL url = new URL("http://szit.hu");
		URLConnection con = url.openConnection();
		stream = new InputStreamReader(con.getInputStream());
		reader = new BufferedReader(stream);
 
		System.out.println(reader.readLine());
		System.out.println(reader.readLine());
 
		for (String line = reader.readLine(); line != null; line = reader.readLine()) {
			System.out.println(line);
		}		
 
	}
 
	public static void main(String args[]) 	{
		Program01 program01 = new Program01();
		program01.getWebpage();
	}
}

Linkek

oktatas/programozas/java/java_web.txt · Utolsó módosítás: 2023/08/24 20:23 szerkesztette: admin