分类: Java
2007-02-13 10:49:50
URL url = new URL (""); |
// URLDemo1.java import java.io.*; import java.net.*; class URLDemo1 { public static void main (String [] args) throws IOException { if (args.length != 1) { System.err.println ("usage: java URLDemo1 url"); return; } URL url = new URL (args [0]); System.out.println ("Authority = "+ url.getAuthority ()); System.out.println ("Default port = " +url.getDefaultPort ()); System.out.println ("File = " +url.getFile ()); System.out.println ("Host = " +url.getHost ()); System.out.println ("Path = " +url.getPath ()); System.out.println ("Port = " +url.getPort ()); System.out.println ("Protocol = " +url.getProtocol ()); System.out.println ("Query = " +url.getQuery ()); System.out.println ("Ref = " +url.getRef ()); System.out.println ("User Info = " +url.getUserInfo ()); System.out.print ('\n'); InputStream is = url.openStream (); int ch; while ((ch = is.read ()) != -1) System.out.print ((char) ch); is.close (); } } |
Authority = Default port = 80 File = /articles/articles.html Host = Path = /articles/articles.html Port = -1 Protocol = http Query = null Ref = null User Info = null <html> <head> <title> Java Jeff - Articles </title> <meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1"> <meta name=author content="Jeff Friesen"> <meta name=keywords content="java, virtual machine"> <script language=JavaScript> if (navigator.appName == "Netscape") document.write ("<br>"); </script> </head> <body bgcolor=#000000> <center> <table border=1 cellpadding=5 cellspacing=0> <tr> <td> <table cellpadding=0 cellspacing=0> <tr> <td> <a href=informit/informit.html> <img alt=InformIT border=0 src=informit.gif></a> </td> </tr> </table> </td> <td align=middle> <img src=title.gif><br> <a href=../welcome/welcome.html> <img alt="Welcome to Java Jeff!" border=0 src=jupiter.jpg> </a><br> <img src=../common/clear_dot.gif vspace=5><br> <a href=../ads/ads.html> <img alt="Welcome to Java Jeff!" border=0 src=jupiter.jpg> </td> <td> <table cellpadding=0 cellspacing=0> <tr> <td> <a href=javaworld/javaworld.html> <img alt=JavaWorld border=0 src=javaworld.gif></a> </td> </tr> </table> </td> </tr> </table> </center> <br> <font color=#ffffff> <center> Best viewed at a resolution of 1024x768 or higher.<br> <img src=../common/clear_dot.gif vspace=5><br> <i> Copyright © 2001-2002, Jeff Friesen. All rights reserved. </i> <p> <a href=../index.html> <img alt=Back border=0 src=../common/back.gif></a> </center> </font> </body> </html> |
InputStream is = url.openStream (); BufferedReader br = new BufferedReader (new InputStreamReader (is)); String line; while ((line = br.readLine ()) != null) System.out.println (line); is.close (); |
URL url = new URL (args [0]); Object o = url.getContent (); if (o instanceof ImageProducer) { ImageProducer ip = (ImageProducer) o; // ... } |
// URLDemo2.java import java.io.*; import java.net.*; import java.util.*; class URLDemo2 { public static void main (String [] args) throws IOException { if (args.length != 1) { System.err.println ("usage: java URLDemo2 url"); return; } URL url = new URL (args [0]); // 返回代表某个资源的连接的新的特定协议对象的引用 URLConnection uc = url.openConnection (); // 进行连接 uc.connect (); // 打印多种头部字段的内容 Map m = uc.getHeaderFields (); Iterator i = m.entrySet ().iterator (); while (i.hasNext ()) System.out.println (i.next ()); // 如果资源允许输入和输出操作就找出来 System.out.println ("Input allowed = " +uc.getDoInput ()); System.out.println ("Output allowed = " +uc.getDoOutput ()); |
Date=[Sun, 17 Feb 2002 17:49:32
GMT] Connection=[Keep-Alive] Content-Type=[text/html; charset=iso-8859-1] Accept-Ranges=[bytes] Content-Length=[7214] null=[HTTP/1.1 200 OK] ETag=["4470e-1c2e-3bf29d5a"] Keep-Alive=[timeout=15, max=100] Server=[Apache/1.3.19 (Unix) Debian/GNU] Last-Modified=[Wed, 14 Nov 2001 16:35:38 GMT] Input allowed = true Output allowed = false |
String result = URLEncoder.encode ("a b"); |
// URLDemo3.java import java.io.*; import java.net.*; class URLDemo3 { public static void main (String [] args) throws IOException { // 检查最后两个参数和参数的数量 if (args.length < 2 || args.length % 2 != 0) { System.err.println ("usage: java URLDemo3 name value " + "[name value ...]"); return; } // 建立程序连接服务器程序资源的URL对象,它返回一个窗体的名称/值对 URL url; url = new URL ("~nabg/echo.cgi"); // 向某个特定协议对象返回表现http资源连接的引用 URLConnection uc = url.openConnection (); // 验证连接的类型,必须是HttpURLConnection的 if (!(uc instanceof HttpURLConnection)) { System.err.println ("Wrong connection type"); return; } // 表明程序必须把名称/值对输出到服务器程序资源 uc.setDoOutput (true); // 表明只能返回有用的信息 uc.setUseCaches (false); //设置Content-Type头部指示指定URL已编码数据的窗体MIME类型 uc.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); // 建立名称/值对内容发送给服务器 String content = buildContent (args); //设置Content-Type头部指示指定URL已编码数据的窗体MIME类型 uc.setRequestProperty ("Content-Length", "" + content.length ()); // 提取连接的适当的类型 HttpURLConnection hc = (HttpURLConnection) uc; // 把HTTP请求方法设置为POST(默认的是GET) hc.setRequestMethod ("POST"); // 输出内容 OutputStream os = uc.getOutputStream (); DataOutputStream dos = new DataOutputStream (os); dos.writeBytes (content); dos.flush (); dos.close (); // 从服务器程序资源输入和显示内容 InputStream is = uc.getInputStream (); int ch; while ((ch = is.read ()) != -1) System.out.print ((char) ch); is.close (); } static String buildContent (String [] args) { StringBuffer sb = new StringBuffer (); for (int i = 0; i < args.length; i++) { // 为正确的传输对参数编码 String encodedItem = URLEncoder.encode (args [i]); sb.append (encodedItem); if (i % 2 == 0) sb.append ("="); // 分离名称和值 else sb.append ("&"); // 分离名称/值对 } // 删除最后的 & 间隔符 sb.setLength (sb.length () - 1); return sb.toString (); } } |
<html> <head> <title>Echoing your name value pairs</title> </head> <body> <ol> <li>name1 : value1 <li>name2 : value2 <li>name3 : value3 </ol> <hr> Mon Feb 18 08:58:45 2002 </body> </html> |