【Java精品源码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了Java精品源码-【精品】java网络 - 其它资料的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
Chapter 7 建立
JAVA网路程式7 本章学习重点 InetAddress 类别 URL类别 URLConnection 类别 HttpURLConnection 类别 URLDecoder和URLEncoder 类别7 InetAddress 类别 InetAddress 物件查询DNS server 查询InetAddress 物件性质的方法 三种物件方法7 InetAddress 三种物件方法 方法 语法 说明 getAllByName Public static InetAddress 输入主机名称,回传一个 getAllByName String InetAddress 物件的阵列,裏面 hostName throws 包含所有的相对的 IP 位址 UnknownHostException getByName Public static InetAddress 输入主机名称,回传一个 getByNameString hostName InetAddress 物件,包含主要的 throws UnknowHostException IP 位址 getLocalHost Public static InetAddress 回传一个 InetAddress 物件, getLocalHost throws 内容为本机端的 IP 地址 UnknownHostException ※回传所得的InetAddress物件包含两种资料 — Host Name IP address7 InetAddress 范例程式一 一个主机名称对一个IP位址,这个 范例主要是练习 InetAddress 物件中 的getByName,由键盘输入已知的 主机名称,若此主机存在,则从萤 幕上可自动的得到网路位址。
若此 主机不存在,也可得知找不到的讯 息。
7 InetAddress 范例程式一 1 // 2 //Ch6_1_1.
java 3 //键盘输入主机名称然后得到网路位址 4 // 5 import
java.net. 6 import
java.io. 7 8 public class Ch6_1_1 9 public static void mainString args throws IOException 10 String gladdress null 11 String temp null 12 13 try 14 //读取一个主机名称字串 15 System.out.printlnquot请输入主机名称:quot 16 temp new BufferedReadernew InputStreamReaderSystem.in.readLine 17 18 temp temp.trim 19 gladdress temp //global variable 20 21 //查询地址 22 InetAddress address InetAddress.getByNametemp 23 24 System.out.printlnquot相关主机的位址是:quot 25 System.out.printlnaddress 26 27 catch UnknownHostException e 7 28 29 System.out.printlnquot找不到输入相关主机的位址 gt quot gladdress 30 31 EX. Ch7_1_1.
java InetAddress 范例程式二 若一个主机名称拥有多个IP位址时 Inetaddress物件的getAllbyname方 法便可查询所有IP位址 操作者输入的主机名称,此范例会 列出所有相对应的IP位址7 InetAddress 范例程式二 1 // 2 //Ch6_1_3.
java 3 然 后 // 键 盘 输 入 一 个 主 机 名 称 得 到 多 个 网 路 位 址 4 // 5 import
java.net. 6 import
java.io. 7 8 public class Ch6_1_3 9 public static void mainString args throws IOException 10 String gladdress null 11 String temp null 12 13 try 14 // 读 取 一 个 主 机 名 称 字 串 15 quot System.out.printlnquot 请 输 入 主 机 名:称 16 temp new BufferedReadernew InputStreamReaderSystem.in.readLine 17 18 temp temp.trim 19 gladdress temp //global variable 20 21 // 查 询 地 址 22 InetAddress address InetAddress.getAllByNametemp 23 quot : 是 System.out.printlnquot 所 有 相 关 主 机 的 位 址 24 25 forint i 0 i lt address.length i 26 System.out.printlnaddressi 27 28 29 catch UnknownHostException e 7 30 位 quot addr System.out.printlnquot 找 不 到 输 入 相 关 主 机 的 gt 址 gl ess 31 32 33 EX. Ch7_1_2.
java InetAddress 范例程式三 ※如何取出InetAddress物件所包含的两种资料 — Host Name IP address 此范例乃是要让读者了解 – getHostName 取得InetAddress内的Host Name – getHostAddress 取得InetAddress内的IP – getAddress 取得4 bytesIP address型态为byte7 InetAddress 范例程式三 1 // 2 //Ch6_1_4.
java 3 H A // 测 试 get ost ddr ess 4 //getAddress H N 和 get ost am e 5 // 6 import
java.net. 7 import
java.io. 8 9 public class Ch6_1_4 10 public static void mainString args throws IOException 11 String gladdress null 12 String temp null 13 14 try 15 I P // 测 试 自 身 位 址 16 InetAddress mypc InetAddress.getLocalHost 17 String address mypc.getHostAddress 18 P I System.out.printlnquotn所 在 的 位 址 是 quot address 19 20 P I //byte型 态 的 位 址 21 byte byteAddress mypc.getAddress 22 P I eA System.out.printlnquotnbyte型 态 的 位 址 是 quot byt ddress 23 24 // 测 试 自 身 主 机 名 称 25 InetAddress hostname InetAddress.getByNameaddress 26 quot System.out.printlnquotn所 在 的 主 机 名 称 是host get N nam e. H ost am e 27 28 catch UnknownHostException e 29 位 quot addr System.out.printlnquot 找 不 到 输 入 相 关 主 机 的 gt 址 gl ess 30 31 7 32 EX. Ch7_1_3.
java URL类别 URLUniform Resource Locator的功用 – 在网际网路上唯一识别位置的位址 URL包含 – 协定protocol型态 – 主机名称 – 电脑接口号码port number – 档案名称 – 锚anchor Ex. – http://www.abc.com.tw:8080/index.htmlpage1 – 一般port会根据不同协定而有不同的预设值,如http 的port为80、ftp的port为217 URL类别继承 如下为URL类别的继承相关图 URL 摸 挡篶 Object URL URLConnection URLDecoder URLEncoder HttpURLConnection JarURLConnection7 EX. Ch7_2_1.
java URLConnection类别 URL 摸 挡篶 Object URL URLConnection URLDecoder URLEncoder HttpURLConnection JarURLConnection URL Connection类别,是介於URL 和应用软体之间的桥梁。
7 可利用URL物件openConnection 方法来产生。
URLConnection类别范例 测试URLConnection类别中的 – getHeaderField – getHeaderFieldKey – getDoInput – getDoOutput – getUseCaches – getAllowUserInteraction – getDefaultAllowUserInteraction 相关 方法的使用。
7 URLConnection类别范例 1 import
java.io. 2 import
java.net. 3 4 public class Ch6_2_2 5 public Ch6_2_2 6 try 7 页 //建 立 包 含 网 U R L的 U R L物 件 8 URL urlnew URLquothttp://home.gigigaga.com/georgehs/JavaBook/index.htmquot 9 10 //产 生 与 网 页 的 连 结 11 URLConnection urlCon url.openConnection 12 13 for int i 1 i 14 String headerKey urlCon.getHeaderFieldKeyi 15 if headerKey null 16 break 17 18 System.out.printlnquot Header quot i quot: quot 19 NAME 20 urlCon.getHeaderFieldi 21 22 U //确 认 R LC onnecti on的 使 用 状 况 23 boolean getIn urlCon.getDoInput 24 boolean getOut urlCon.getDoOutput 25 U System.out.printlnquot 是 否 R LC onnecti on写 入 quot getIn 26 System.out.printlnquot U on读 出 quot getO ut 是 否 R LC onnecti 27 28 //Caches 的 使 用 状 况7 29 boolean getCaches urlCon.getUseCaches 30 U System.out.printlnquot 是 否 R LC onnecti on使 用 中 quot getC aches URLConnection类别范例 31 32 //使 用 者 互 动 的 测 试 33 boolean getAUI urlCon.getAllowUserInteraction 34 boolean getDAUI urlCon.getDefaultAllowUserInteraction 35 动 System.out.printlnquot 是 否 允 许 互 quot getA U I 36 System.out.printlnquot quot 是 否 初 始 允 许 互 动 getD A U I 37 38 //将U R LC onnecti ng on传 回 S tri 39 String urlStr urlCon.toString 40 :quot S System.out.printlnquot 内 容 字 串 url tr 41 42 catchMalformedURLException me 43 System.out.printlnquotError: quot me 44 catch IOException ioe 45 System.out.printlnquotError: quot ioe 46 47 48 49 public static void mainString args 50 Ch6_2_2 tu new Ch6_2_2 51 52 53 7 EX. Ch7_2_2.
java HttpURLConnection类别 HTTP URLConnection类别是URL Connection的子类别。
URL 摸 挡篶 Object URL URLConnection URLDecoder URLEncoder HttpURLConnection JarURLConnection7 HttpURLConnection类别范例 测试HttpURLConnection类别 setRequestMethod, getRequestMethod, getLastModified, getFollowRedirects, getResponseCode, getResponseMessage相关方法的使 用7 HttpURLConnection类别范例 1 Import
java.net. 2 Import
java.io. 3 Import
java.util. 4 5 Public class Ch6_3_1 6 public static void mainString args 7 Try 8 URL url new URLquothttp://home.gigigaga.com/georgehs/JavaBook/index.htmquot 9 URLConnection conn url.openConnection 10 HttpURLConnection httpConn HttpURLConnection conn 11 12 H //藉 由 E A D 来 获 得 最 后 修 改 日 期 13 httpConn.setRequestMethodquotHEADquot 14 System.out.printlnurl quot was last modified at quot 15 System.out.printlnnew DatehttpConn.getLastModified 16 17 //HTTP的 需 求 方 法 18 String requestMethod httpConn.getRequestMethod 19 法 System.out.printlnquotHTTP联 结 的 需 求 方 quot requestM ethod 20 21 //是 否 为 追 随 重 导 方 法 22 boolean followRedirects httpConn.getFollowRedirects 23 向 olow rects System.out.printlnquot 是 否 为 自 动 转 quot f l R edi 24 25 E //找 的 对 应 值 rror 404 26 try 27 int responseCode httpConn.getResponseCode 28 ifresponseCode HttpURLConnection.HTTP_OK 7 29 30 InputStream in htt.
上一篇:
【精品】JAVA动态代理及在HSF中的应用
下一篇:
英文课程作业:电影库(Piddington library )建立方案分析