Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.

Similar presentations


Presentation on theme: "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."— Presentation transcript:

1 CSCE 515: Computer Network Programming Chin-Tser Huang huangct@cse.sc.edu University of South Carolina

2 4/1/20042 URL Classes A framework for easily accessing and decoding URL resources Enable an application to create URL object using standard URL addressing scheme Then call getContent() method to download and decode the addressed object Two chains of control Protocol chain Content chain

3 4/1/20043 URL Chains of Control URL URLStreamHandler URLConnection URLStreamHandlerFactory HTTP / FTP / … http://x/y.gif URLConnection ContentHandler ContentHandlerFactory gif / au / … Protocol chainContent chain

4 4/1/20044 Class URL Constructors URL(String url) throws MalformedURLException URL(String protocol, String host, String file) throws MalformedURLException URL(String protocol, String host, int port, String file) throws MalformedURLException URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException URL(URL context, String spec) throws MalformedURLException URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException

5 4/1/20045 Class URL Methods String getProtocol() String getHost() int getPort() String getFile() String getRef() boolean sameFile(URL other) String toExternalForm() URLConnection openConnection() throws IOException InputStream openStream() throws IOException Object getContent() throws IOException protected void set(String protocol, String host, int port, String file, String ref) static void setURLStreamHandlerFactory(URLStreamHandlerFactory factory)

6 4/1/20046 Class URL Exceptions IOException MalformedURLException

7 4/1/20047 Example of Using URL Class URL url = new URL (http://java.sun.com/index.html); InputStream in = url.openStream (); Reader reader = new InputStreamReader (in, “latin1”); BufferedReader bufferedReader = new BufferedReader (reader); PrintWriter console = new PrintWriter (System.out); String line; while ((line = bufferedReader.readLine ())_ != null) console.println (line); console.flush(); bufferedReader.close ();

8 4/1/20048 Class URLConnection Constructor protected URLConnection(URL url)

9 4/1/20049 Class URLConnection Methods URL getURL() void setDoInput(boolean doInput) boolean getDoInput() void setDoOutput(boolean doOutput) boolean getDoOutput() void setAllowUserInteraction(boolean allow) boolean getAllowUserInteraction() void setUseCaches(boolean use) boolean getUseCaches() void setRequestProperty(String key, String value) String getRequestProperty(String key) void setIfModifiedSince(long ifModified) long getIfModifiedSince() abstract void connect() throws IOException

10 4/1/200410 Class URLConnection Methods OutputStream getOutputStream() throws IOException InputStream getInputStream() throws IOException Object getContent() throws IOException String getHeaderField(String name) String getHeaderFieldKey(int n) String getHeaderField(int n) int getHeaderFieldInt(String name, int alt) long getHeaderFieldDate(String name, long alt) int getContentLength() String getContentType() String getContentEncoding() long getExpiration() long getDate() long getLastModified()

11 4/1/200411 Class URLConnection Static methods void setDefaultAllowUserInteraction(boolean defaultAllow) boolean getDefaultAllowUserInteraction() void setDefaultRequestProperty(String key, String value) String getDefaultRequestProperty(String key) protected String guessContentTypeFromName(String fname) String guessContentTypeFromStream(InputStream is) FileNameMap getFileNameMap() void setFileNameMap(FileNameMap map) void setContentHandlerFactory(ContentHandlerFactory factory)

12 4/1/200412 Class URLConnection Variables protected URL url protected boolean doInput protected boolean doOutput protected boolean allowUserInteraction protected boolean useCaches protected long ifModifiedSince protected boolean connected Exceptions IOException

13 4/1/200413 Example of Using URLConnection Class // String user, password; URL url = new URL (getCodeBase (), “/cgi-bin/update.cgi”); URLConnection connection = url.openConnection (); connection.setDoOutput (true); OutputStream out = connection.getOutputStream (); Writer writer = new OutputStreamWriter (out, “latin1”); writer.write (“user=” + URLEncoder.encode (user)); writer.write (“&password=” + URLEncoder.encode (password)); writer.close (); InputStream in = connection.getInputStream (); Reader reader = new InputStreamReader (in, “latin1”); BufferedReader bufferedReader = new BufferedReader (reader); PrintWriter console = new PrintWriter (System.out); String line; while ((line = bufferedReader.readLine ())_ != null) console.println (line); console.flush(); bufferedReader.close ();

14 4/1/200414 Class HttpURLConnection Constructor protected HttpURLConnection(URL url)

15 4/1/200415 Class HttpURLConnection Methods void setRequestMethod(string method) throws ProtocolException String getRequestMethod() int getResponseCode() throws IOException String getResponseMessage() throws IOException abstract void disconnect() boolean abstract usingProxy()

16 4/1/200416 Class HttpURLConnection Static methods void setFollowRedirects(boolean set) boolean getFollowRedirects()

17 4/1/200417 Class HttpURLConnection Variables protected String method protected int responseCode protected String responseMessage Static variables Success codes Redirect codes Client error codes Server error codes

18 4/1/200418 Example of HttpURLConnection Class URL url = new URL (getCodeBase (), “/cgi-bin/update.cgi”); URLConnection conn = url.openConnection (); HttpURLConnection httpConn = (HttpURLConnection) conn; int responseCode = httpConn.getResponseCode (); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream in = httpConn.getInputStream (); Reader reader = new InputStreamReader (in, “latin1”); BufferedReader bufferedReader = new BufferedReader (reader); PrintWriter console = new PrintWriter (System.out); String line; while ((line = bufferedReader.readLine ())_ != null) console.println (line); console.flush(); bufferedReader.close (); }

19 4/1/200419 Interface URLStreamHandlerFactory Method URLStreamHandler createURLStreamHandler(String protocol)

20 4/1/200420 Class URLStreamHandler Create an appropriate instance of URLConnection class capable of serving a particular protocol Method protected abstract URLConnection openConnection(URL url) throws IOException protected void parseURL(URL url, String spec, int start, int limit) protected String toExternalForm(URL url) protected void setURL(URL url, String protocol, String host, int port, String file, String ref)

21 4/1/200421 Interface ContentHandlerFactory Method ContentHandler createContentHandler(String mimetype)

22 4/1/200422 Class ContentHandler Decode content of a URL and produce result that is usable by Java run time Method abstract Object getContent(URLConnection connection) throws IOException

23 4/1/200423 An HTTP Protocol Handler Example Examine headers sent by server to determine type of file and to invoke appropriate handler URL getContent() HTTPURLStreamHandler openConnection() HTTPURLConnection getInputStream() URLStreamHandlerFactoryImpl createURLStreamHandler() URLConnection getContent() TextPlainContentHandler getContent() ContentHandlerFactoryImpl createContentHandler() PageViewer main()

24 4/1/200424 Class URLStreamHandlerFactoryImpl /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class URLStreamHandlerFactoryImpl implements URLStreamHandlerFactory { public URLStreamHandler createURLStreamHandler (String protocol) { if (protocol.equalsIgnoreCase ("http")) return new HTTPURLStreamHandler (); else if (protocol.equalsIgnoreCase ("finger")) return new FingerURLStreamHandler (); else if (protocol.equalsIgnoreCase ("rawfinger")) return new RawFingerURLStreamHandler (); else return null; }

25 4/1/200425 Class HTTPURLStreamHandler /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class HTTPURLStreamHandler extends URLStreamHandler { protected URLConnection openConnection (URL url) throws IOException { return new HTTPURLConnection (url); }

26 4/1/200426 Class HTTPURLConnection /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; import java.util.*; Import java.text.*; public class HTTPURLConnection extends URLConnection { // public HTTPURLConnection (URL url) … // public void setRequestProperty (String name, String value) … // public String getRequestProperty (String name) … // public synchronized void connect () throws IOException … // public InputStream getInputStream () throws IOException … // public String getHeaderFieldKey (int index) … // public String getHeaderField (int index) … // public String getHeaderField (String key) … }

27 4/1/200427 Constructor HTTPURLConnection protected Hashtable requestProperties; protected Vector keys; protected Hashtable headers; public HTTPURLConnection (URL url) { super (url); requestProperties = new Hashtable (); keys = new Vector (); headers = new Hashtable (); }

28 4/1/200428 Method setRequestProperty public void setRequestProperty (String name, String value) { if (connected) throw new IllegalStateException ("Already connected."); requestProperties.put (name, value); } public String getRequestProperty (String name) { return (String) requestProperties.get (name); }

29 4/1/200429 Method connect protected InputStream in; public synchronized void connect () throws IOException { if (!connected) { String host = url.getHost (); int port = (url.getPort () == -1) ? 80 : url.getPort (); Socket socket = new Socket (host, port); sendRequest (socket.getOutputStream ()); in = new BufferedInputStream (socket.getInputStream ()); readHeaders (); connected = true; } // protected void sendRequest (OutputStream out) throws IOException … // protected void readHeaders () throws IOException …

30 4/1/200430 Method sendRequest protected void sendRequest (OutputStream out) throws IOException { setRequestProperty ("User-Agent", "JNP-HTTP/2e"); if (ifModifiedSince != 0) { Date since = new Date (ifModifiedSince); SimpleDateFormat formatter = new SimpleDateFormat ("EEE, d MMM yyyy hh:mm:ss z"); formatter.setTimeZone (TimeZone.getTimeZone ("GMT")); setRequestProperty ("If-Modified-Since", formatter.format (since)); } StringBuffer request = new StringBuffer ("GET "); request.append (URLEncoder.encode (url.getFile ())); request.append (" HTTP/1.0\r\n"); Enumeration keys = requestProperties.keys (); while (keys.hasMoreElements ()) { String key = (String) keys.nextElement (); request.append (key); request.append (": "); request.append (requestProperties.get (key)); request.append ("\r\n"); } request.append ('\n'); out.write (request.toString ().getBytes ("latin1")); }

31 4/1/200431 Method readHeaders protected void readHeaders () throws IOException { String status = readLine (); String header; while (((header = readLine ()) != null) && (!header.trim ().equals (""))) { int colon = header.indexOf (":"); if (colon >= 0) { String key = header.substring (0, colon).trim (); String value = header.substring (colon + 1).trim (); keys.addElement (key); headers.put (key.toLowerCase (), value); } // protected String readLine () throws IOException …

32 4/1/200432 Method readLine protected String readLine () throws IOException { StringBuffer result = new StringBuffer (); int chr; while (((chr = in.read ()) != -1) && (chr != 10) && (chr != 13)) result.append ((char) chr); if ((chr == -1) && (result.length () == 0)) return null; if (chr == 13) { in.mark (1); if (in.read () != 10) in.reset (); } return result.toString (); }

33 4/1/200433 Method getInputStream public InputStream getInputStream () throws IOException { if (!doInput) throw new IllegalStateException ("Input disabled."); connect (); return in; }

34 4/1/200434 Methods to Read Headers public String getHeaderFieldKey (int index) { if (!connected) throw new IllegalStateException ("Not connected."); if (index < keys.size ()) return (String) keys.elementAt (index); else return null; } public String getHeaderField (int index) { if (!connected) throw new IllegalStateException ("Not connected."); if (index < keys.size ()) return getHeaderField ((String) keys.elementAt (index)); else return null; } public String getHeaderField (String key) { if (!connected) throw new IllegalStateException ("Not connected."); return (String) headers.get (key.toLowerCase ()); }

35 4/1/200435 Class ContentHandlerFactoryImpl /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class ContentHandlerFactoryImpl implements ContentHandlerFactory { public ContentHandler createContentHandler (String mimeType) { if (mimeType.equalsIgnoreCase ("text/plain")) return new TextPlainContentHandler (); else return null; }

36 4/1/200436 Class TextPlainContentHandler /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class TextPlainContentHandler extends ContentHandler { public Object getContent (URLConnection connection) throws IOException { InputStream in = connection.getInputStream (); Reader reader = new InputStreamReader (in, "latin1"); BufferedReader bufferedReader = new BufferedReader (reader); StringBuffer content = new StringBuffer (); String line; while ((line = bufferedReader.readLine ()) != null) content.append (line).append ('\n'); bufferedReader.close (); return content.toString (); }

37 4/1/200437 Class PageViewer /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; public class PageViewer { public static void main (String args[]) throws IOException { if (args.length != 1) throw new IllegalArgumentException ("Usage: PageViewer "); URL.setURLStreamHandlerFactory (new URLStreamHandlerFactoryImpl ()); URLConnection.setContentHandlerFactory (new ContentHandlerFactoryImpl ()); URL url = new URL (args[0]); System.out.println (url.getContent ()); }

38 4/1/200438 Next Class Remote method invocation (RMI) Read JNP Ch. 23


Download ppt "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."

Similar presentations


Ads by Google