This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
java:urls [2015/03/23 20:18] gthanos created |
java:urls [2015/03/23 20:51] gthanos [Χρησιμοποιώντας την κλάση URL για να ανοίξουμε ένα απομακρυσμένο resource] |
||
---|---|---|---|
Line 21: | Line 21: | ||
Όλοι οι κατασκευαστές της κλάσης URL μπορούν να δημιουργήσουν ένα [[http://docs.oracle.com/javase/7/docs/api/java/net/MalformedURLException.html|MalformedURLException]] εάν το πρωτόκολλο που εισάγεται δεν είανι σωστό ή ένα από τα ορίσματα έχει τιμή NULL. Θα πρέπει να διαχειριστείτε το συγκεκριμένο Exception μέσε ένα try/catch block. | Όλοι οι κατασκευαστές της κλάσης URL μπορούν να δημιουργήσουν ένα [[http://docs.oracle.com/javase/7/docs/api/java/net/MalformedURLException.html|MalformedURLException]] εάν το πρωτόκολλο που εισάγεται δεν είανι σωστό ή ένα από τα ορίσματα έχει τιμή NULL. Θα πρέπει να διαχειριστείτε το συγκεκριμένο Exception μέσε ένα try/catch block. | ||
+ | ===== Αναλύοντας ένα URL με βάση το API της κλάσης ===== | ||
+ | |||
+ | <code java DecomposeURL.java> | ||
+ | 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()); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Το αποτέλεσμα εκτέλεσης του παραπάνω κώδικα είναι το εξής | ||
+ | <code> | ||
+ | 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 | ||
+ | </code> | ||
+ | |||
+ | ===== Χρησιμοποιώντας την κλάση URL για να ανοίξουμε ένα απομακρυσμένο resource ===== | ||
+ | |||
+ | <code java URLReader.java> | ||
+ | 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() ); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | To παραπάνω θα μπορούσε να γραφεί ισοδύναμα ως εξής με χρήση ενός αντικειμένου URLConnection | ||
+ | <code java URLConnectionReader.java> | ||
+ | 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() ); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||