This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
java:urls [2015/03/23 20:25] gthanos [Αναλύοντας ένα URL με βάση το API της κλάσης] |
java:urls [2015/03/23 20:53] gthanos [Χρησιμοποιώντας την κλάση URL για να ανοίξουμε ένα απομακρυσμένο resource] |
||
---|---|---|---|
Line 41: | Line 41: | ||
System.out.println("filename = " + aURL.getFile()); | System.out.println("filename = " + aURL.getFile()); | ||
System.out.println("ref = " + aURL.getRef()); | System.out.println("ref = " + aURL.getRef()); | ||
- | System.out.println("userInfo = " + aURL.getUserInfo() ); | ||
} | } | ||
} | } | ||
</code> | </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 παραπάνω θα μπορούσε να γραφεί ισοδύναμα ως εξής με χρήση ενός αντικειμένου [[http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html|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> | ||