Click here to load reader

자바 네트워크 프로그래밍 (URL,URLEncoder) 프로그래밍 언어 실험실 석사 3 학기 조성대

Embed Size (px)

DESCRIPTION

PL Lab3 Java TM 2 Platform Standard Edition java.lang.Object java.net javax.swing java.appletjava.awtjava.io …… InetAddressSocketURLServerSocket …… URLEncoder ……

Citation preview

(URL,URLEncoder) 3 PL Lab2 URL URLEncoder PL Lab3 Java TM 2 Platform Standard Edition java.lang.Object java.net javax.swing java.appletjava.awtjava.io InetAddressSocketURLServerSocket URLEncoder PL Lab4 java.net classes interfaces Classes AuthenticatorContentHandler DatagramPacketDatagramSocket DatagramSocketImplHttpURLConnection InetAddressJarURLConnection MulticastSocketNetPermission PasswordAuthenticationServerSocket SocketSocketImpl SocketPermissionURL URLClassLoaderURLConnection URLDecoderURLEncoder URLStreamHandle Interfaces ContentHandlerFactoryFileNameMap SocketImplFactorySocketOptions URLStreamHandlerFactory jdk1.2 PL Lab5 java.net.URL Constructors URL( ) : 6 constructor . Methods equals()getContent() getFile()getHost() getPort()getProtocol() getRef()hashCode() openConnection()openStream() sameFile()set() setURLStreamHandlerFactory()toExternalForm() toString() PL Lab6 URL java.net.URL URL (Uniform resource locator) URL URL + PL Lab7 URL URL(String spec) URL(String protocol, String host, String file) URL(String protocol, String host, int port, String file) URL(String protocol, String host, int port, String file, URLStreamHandler handler) URL(URL context, String spec) URL(URL context, String spec, URLStreamHandler handler) new URL( ) URL MalformedURLException URL . HTTP,FTP,news, mailto, gopher, file PL Lab8 URL public URL(String url) throws MalformedURLException import java.net.*; public class URLConstructorTest1 { public static void main(String[] args) { URL webURL,ftpURL; try { webURL = new URL( ); System.out.println(webURL); ftpURL = new URL( ftp://ftp.macfaq.com/pub/ ); System.out.println(ftpURL); } Catch (MalformedURLException e) { System.err.println(e); }} } java.net.MalformedURLException: ftp//ftp.macfaq.com/pub/:java.lang.Exception PL Lab9 URL public URL(String protocol, String host, String file) throws MalformedURLException 1 file (/) try { URL u = new URL( http ,www.macfaq.com, /blueribbon.html#intro ); } catch (MalformedURLException e) { System.out.println(e); } PL Lab10 URL public URL(String protocol, String host, int port, String file) throws MalformedURLException try { URL u = new URL( http , ,80, /blueribbon.html#intro ); } catch ( MalformedURLException e) { System.err.println(e); } PL Lab11 URL public URL(URL u, String s) throws MalformedURLException URL URL user/public_html/html/examples.html user/public_html/html/vendor.html user/public_html/applet/URLConstructorTest4.class examples.html PL Lab12 URL import java.net.*; import java.applet.Applet; public class URLConstructorTest4 extends Applet { public void init() { URL u1, u2; u1 = getDocumentBase(); System.out.println(u1); try {u2 = new URL(u1, vendor.html ); System.out.println(u2); } Catch (MalformedURLException e) { System.err.println(e); } } } File: user/public_html/html/examples.html File: user/public_html/html/vendor.html PL Lab13 URL URL class Methods Protocol : getProtocol() Host : getHost() Port : getPort() File : getFile() Ref : getRef() URLStreamHandler PL Lab14 URL class Methods public String getProtocol() URL String . public String getHost() URL String . public String getPort() URL int . 1 PL Lab15 URL class Methods public String getFile() URL String . (/) (#) / public String getRef() URL . URL Null . PL Lab16 URL class Methods import java.net.*; public class getURLParts { public static void main(String args[]) { try { URL u = new URL(args[0]); System.out.println( u ); System.out.println( u.getProtocol() ); System.out.println( u.getHost() ); System.out.println( u.getPort() ); System.out.println( u.getFile() ); System.out.println( u.getRef() ); } catch (MalformedURLException e ) { System.out.println( e ); } } } $ java getURLParts http/demoweb/html-primer.html A1.3.3 PL Lab17 URL public final InputStream openStream() throws IOException URL (handshaking) InputStream HTML . URL u = new URL( ); DataInputStream theHTML = newDataInputStream(u.openStream()); While ( ( thisLine = theHTML.readLine()) != null ) System.out.println(this.Line); PL Lab18 URL public URLConnection openConnection() throws IOException URL URLConnection . . URLConnection 10 URL u = new URL( ); URLConnection uc = u.openConnection(); PL Lab19 URL public final Object getContent() throws IOException URL . . ASCII HTML InputStream GIF/JPEG ImageProducer MIME content-type Content-type ClassNotfoundException URL u = new URL( ); Object o = u.getContent(); URL u = new URL( ); Object o = u.getContent(); PL Lab20 URL public boolean sameFile(URL other) URL URL IP URL URL . URL u1 = new URL( ); URL u2 = new URL( ); If ( u1.samefile(u2) ) { System.out.println( the same ); } else { System.out.println( not the same ); } PL Lab21 URL public boolean equals(Object o) sameFile() . URL URL IP URL . URL u1 = new URL( ); URL u2 = new URL( ); If ( u1.equals(u2) ) { System.out.println( the same ); } else { System.out.println( not the same ); } PL Lab22 java.net.URLEncoder Constructors . Methods static String encode(String s) PL Lab23 URLEncoder URL #, (non-alphanumeric) URL , , % ASC 16 + + %2b java.net.URLEncorder encode() public static String encode(String s) PL Lab24 URLEncoder System.out.println(URLEncoder.encode( This String has spaces )); System.out.println(URLEncoder.encode( This*string*has*stars )); System.out.println(URLEncoder.encode( This+string+has+plus )); System.out.println(URLEncoder.encode( This:string:has:colons )); System.out.println(URLEncoder.encode( This.string.has.periods )); This+string+has+spaces This%2astring%2ahas%2astars This%2bstring%2bhas%2bplus This%3astring%3ahas%3acolons This%2estring%2ehas%2eperiods PL Lab25 GET CGI URLRequestor PL Lab26 CGI . = & username=Elliotte+Rusty+Harold& =elharo%40sunsite%2eedu : username : Elliotte+Rusty+Harold : : PL Lab27 QueryString import java.net.URLEncoder; public class queryString { String query; public queryString(Object name, Object value) { query = URLEncoder.encode(name.toString()) + = + URLencoder.encode(value.toString()); } public void add(Object name, Object value) { query += & + URLEncoder.encode(name.toString()) + = + URLEncoder.encode(value.toString()); } public String toString() { return query; } PL Lab28 QSTest public QSTest { public static void main(String[] args) { String name1 = username ; String value1 = Elliotte Rusty Harold ; String name2 = ; String value2 = ; queryString qs_ = new queryString(name1, value1); qs_.add(name2, value2); System.out.println( qs_.toString() ); } % java QSTest username=Elliotte+Rusty+Harold& =elharo%40sunsite%2eedu PL Lab29 GET CGI CGI PL Lab30 lycos import java.net.*; import java.io.*; public class lycos { public static void main(String[] args) { String querystring = ""; for ( int i =0; i