This is an old revision of the document!
Το πρώτο μέρος του URL ονομάζεται αναγνωριστικό πρωτοκόλλου και δείχνει το τι πρωτόκολλο χρησιμοποιείται κάθε φορά, και το δεύτερο μέρος ονομάζεται όνομα του πόρου και διευκρινίζει τη διεύθυνση IP ή το όνομα τομέα όπου βρίσκεται ο πόρος. Το αναγνωριστικό πρωτοκόλλου και το όνομα του πόρου χωρίζονται από μια άνω και κάτω τελεία και δύο καθέτους.
Μπορείτε να δημιουργήσετε ένα αντικείμενο του τύπου URL με τους παρακάτω τρόπους
URL myURL = new URL("http://example.com/pages/");
URL page1URL = new URL(myURL, "page1.html"); URL page2URL = new URL(myURL, "page2.html");
URL yourURL = new URL("http", "example.com", 80, "pages/page1.html");
Όλοι οι κατασκευαστές της κλάσης URL μπορούν να δημιουργήσουν ένα MalformedURLException εάν το πρωτόκολλο που εισάγεται δεν είανι σωστό ή ένα από τα ορίσματα έχει τιμή NULL. Θα πρέπει να διαχειριστείτε το συγκεκριμένο Exception μέσε ένα try/catch block.
import java.net.*; import java.io.*; public class DecomposeURL { public static void main(String[] args) throws Exception { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=john&sirname=smith#flushing"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost()); System.out.println("port = " + aURL.getPort()); System.out.println("path = " + aURL.getPath()); System.out.println("query = " + aURL.getQuery()); System.out.println("filename = " + aURL.getFile()); System.out.println("ref = " + aURL.getRef()); } }
Το αποτέλεσμα εκτέλεσης του παραπάνω κώδικα είναι το εξής
protocol = http authority = example.com:80 host = example.com port = 80 path = /docs/books/tutorial/index.html query = name=john&sirname=smith filename = /docs/books/tutorial/index.html?name=john&sirname=smith ref = flushing
import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) { URL url=null; try { url = new URL(args.length>0 ? args[0] : "http://feeds.bbci.co.uk/news/rss.xml"); BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream()) ); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } catch(MalformedURLException ex) { System.out.println("Malformed URL: +"+ args[0] ); ex.printStackTrace(); } catch(IOException ex) { System.out.println("Error while reading or writing from URL: "+url.toString() ); } } }
To παραπάνω θα μπορούσε να γραφεί ισοδύναμα ως εξής με χρήση ενός αντικειμένου URLConnection
import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) { URL url=null; try { url = new URL(args.length>0 ? args[0] : "http://feeds.bbci.co.uk/news/rss.xml"); URLConnection urlcon = url.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader(urlcon.getInputStream()) ); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } catch(MalformedURLException ex) { System.out.println("Malformed URL: +"+ args[0] ); ex.printStackTrace(); } catch(IOException ex) { System.out.println("Error while reading or writing from URL: "+url.toString() ); } } }