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:21] gthanos [Δημιουργώντας ένα URL] |
java:urls [2015/03/23 20:51] gthanos [Χρησιμοποιώντας την κλάση URL για να ανοίξουμε ένα απομακρυσμένο resource] |
||
---|---|---|---|
Line 31: | Line 31: | ||
URL aURL = new URL("http://example.com:80/docs/books/tutorial" | URL aURL = new URL("http://example.com:80/docs/books/tutorial" | ||
- | + "/index.html?name=networking#DOWNLOADING"); | + | + "/index.html?name=john&sirname=smith#flushing"); |
System.out.println("protocol = " + aURL.getProtocol()); | System.out.println("protocol = " + aURL.getProtocol()); | ||
Line 45: | Line 45: | ||
</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 παραπάνω θα μπορούσε να γραφεί ισοδύναμα ως εξής με χρήση ενός αντικειμένου URLConnection | ||
+ | <code java URLConnectionReader.java> | ||
+ | <code java URLReader.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> | ||