Presentation is loading. Please wait.

Presentation is loading. Please wait.

2: Application Layer1 Quelques compléments Java réseaux.

Similar presentations


Presentation on theme: "2: Application Layer1 Quelques compléments Java réseaux."— Presentation transcript:

1 2: Application Layer1 Quelques compléments Java réseaux

2 2: Application Layer2 Sommaire r URL r html et swing r URLconnections

3 2: Application Layer3 URL r la classe URL permet d'utiliser les URLURL r les classes URLDecoder et URLEncoder permettent de traiter les formulaires HTMLURLDecoderURLEncoder r Une URI est une sorte de généralisation des URL qui inclut de Uniform Resource Location (URL) mais aussi de Uniform Ressource Names (URN)URI exemples tel: =_hdbc r (la classe URLConnection sera décrite plus loin)

4 2: Application Layer4 Exemple URLSplitterURLSplitter import java.net.*; public class URLSplitter { public static void main(String args[]) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); System.out.println("The URL is " + u); System.out.println("The scheme is " + u.getProtocol()); System.out.println("The user info is " + u.getUserInfo()); String host = u.getHost(); if (host != null) { int atSign =

5 2: Application Layer5 Exemple suite if (atSign != -1) host = host.substring(atSign+1); System.out.println("The host is " + host); } else { System.out.println("The host is null."); } System.out.println("The port is " + u.getPort()); System.out.println("The path is " + u.getPath()); System.out.println("The ref is " + u.getRef()); System.out.println("query string is " + u.getQuery()); } catch (MalformedURLException ex) { System.err.println(args[i] + " is not a URL I understand."); } System.out.println(); } // end for } // end main } // end URLSplitter

6 2: Application Layer6 Ouvrir une URL SourceViewerSourceViewer public class SourceViewer { public static void main (String[] args) { if (args.length > 0) { try { URL u = new URL(args[0]); InputStream in = u.openStream(); in = new BufferedInputStream(in); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } catch (MalformedURLException ex) { System.err.println(args[0] + " URL malformée"); } catch (IOException ex) {System.err.println(ex);} }

7 2: Application Layer7 URLEncode: ExempleExemple public class QueryString { private StringBuffer query = new StringBuffer(); public QueryString(String name, String value) { encode(name, value); } public synchronized void add(String name, String value) { query.append('&'); encode(name, value); } private synchronized void encode(String name, String value){ try { query.append(URLEncoder.encode(name, "UTF-8")); query.append('='); query.append(URLEncoder.encode(value, "UTF-8")); } catch (UnsupportedEncodingException ex) { throw new RuntimeException("??"); } /...

19 2: Application Layer19 OuvertureOuverture d'une page: public class SourceViewer2 { public static void main (String[] args) { if (args.length > 0) { try { URL u = new URL(args[0]); URLConnection uc = u.openConnection(); InputStream raw = uc.getInputStream(); InputStream buffer = new BufferedInputStream(raw); Reader r = new InputStreamReader(buffer); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } catch (MalformedURLException ex) { System.err.println(args[0] + " Malformée URL"); } catch (IOException ex) { System.err.println(ex); } } // end if } // end main

20 2: Application Layer20 Ouverture d'une page Ouverture d'une page (avec codage correct) public class SourceViewer3 { public static void main (String[] args) { for (int i = 0; i < args.length; i++) { try { // set default encoding String encoding = "ISO "; URL u = new URL(args[i]); URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int encodingStart = contentType.indexOf("charset="); if (encodingStart != -1) { encoding = contentType.substring(encodingStart+8); } InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in, encoding); /...

21 2: Application Layer21 Date dernière modification public class DerniereModif { public static void main(String args[]) { for (int i=0; i < args.length; i++) { try { URL u = new URL(args[i]); HttpURLConnection http=(HttpURLConnection)u.openConnection(); http.setRequestMethod("HEAD"); System.out.println(u + "a été modifiée " + new Date(http.getLastModified())); } // end try catch (MalformedURLException ex) { System.err.println(args[i] + " URL??"); } catch (IOException ex) { System.err.println(ex); } } // end for } // end main } // end DernierModif

22 2: Application Layer22 //... int c; while ((c = r.read()) != -1) { System.out.print((char) c); } catch (MalformedURLException ex) { System.err.println(args[0] + " URL?"); } catch (IOException ex) { System.err.println(ex); } } // end if } // end main } // end SourceViewer3

23 2: Application Layer23 Lire les headers public class HeaderViewer { public static void main(String args[]) { for (int i=0; i < args.length; i++) { try { URL u = new URL(args[0]); URLConnection uc = u.openConnection(); System.out.println("Content-type: " + uc.getContentType()); System.out.println("Content-encoding: " + uc.getContentEncoding()); System.out.println("Date: " + new Date(uc.getDate())); System.out.println("Last modified: " + new Date(uc.getLastModified())); System.out.println("Expiration date: " + new Date(uc.getExpiration())); System.out.println("Content-length: " + uc.getContentLength()); } // end try

24 2: Application Layer24 (Suite) //... catch (MalformedURLException ex) { System.err.println(args[i] + "URL?? "); } catch (IOException ex) { System.err.println(ex); } System.out.println(); } // end for } // end main } // end HeaderViewer

25 2: Application Layer25 Afficher le header public class afficheHeaders { public static void main(String args[]) { for (int i=0; i < args.length; i++) { try { URL u = new URL(args[i]); URLConnection uc = u.openConnection(); for (int j = 1; ; j++) { String header = uc.getHeaderField(j); if (header == null) break; System.out.println(uc.getHeaderFieldKey(j) + ": " + header); } // end for } // end try catch (MalformedURLException ex) { System.err.println(args[i] + "URL ???"); } catch (IOException ex) { System.err.println(ex); } System.out.println(); } // end for } // end main } // end afficheHeaders


Download ppt "2: Application Layer1 Quelques compléments Java réseaux."

Similar presentations


Ads by Google