1、1 InetAddress 类import .*;import java.io.*; public class HostLookuppublic static void main(String args)if(args.length0)for(int i=0;iargs.length;i+)System .out.println(lookup(argsi);/命令行带参数则使用参数 elseBufferedReader bin=new BufferedReader(new InputStreamReader(System.in);System.out.println(“请输入主机名或 IP 地
2、址,输入“exit“ 退出程序!“);trywhile(true)System.out.print(“主机名或 IP 地址:“);String host=bin.readLine();/读取键盘输入if(host.equalsIgnoreCase(“exit“) |host.equalsIgnoreCase(“quit“)break;System.out.println(lookup(host);/调用 lookup 方法转换输入的字符catch(IOException e)System.out.println(e);private static String lookup(String ho
3、st)InetAddress addr;tryaddr=InetAddress.getByName(host);catch(UnknownHostException e)return “cannot find host“+host;if(isHostname(host)System.out.print(host+“的 IP 地址为:“);/是主机名return addr.getHostAddress();elseSystem.out.print(host+“的主机名为:“);/是 IP 地址return addr.getHostName();private static boolean isH
4、ostname(String host)char ca=host.toCharArray();for(int i=0;ica.length;i+)if(!Character.isDigit(cai)if(cai!=.)return true;return false;2 URL 类相关代码利用 URL 类获取网络资源import .URL;import java.io.BufferedReader;import java.io.InputStreamReader;public class TestURL public String getContent(String strUrl)/返回字符串
5、,错误则返回“error open url“tryURL url=new URL(strUrl);BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream();String s=“;StringBuffer sb=new StringBuffer(“);while(s=br.readLine()!=null) sb.append(s+“rn“); br.close();return sb.toString();catch(Exception e)return “error open url“ + strU
6、rl;public static void main(String args)TestURL ou=new TestURL();/具体使用方法System.out.println(ou.getContent(“http:/ “); 3 URLConnection 类一个具有 IE 功能的小型浏览器import java.awt.*;import java.awt.event.*;import javax.swing.*; import javax.swing.event.*;import java.io.*; import .*;public class browser extends JFr
7、ameJLabel jlabel;JTextField jtf;JEditorPane edpl;public browser()super(“browser“);Container con=getContentPane();jlabel=new JLabel(“请输入要访问的网址:“);jtf=new JTextField(“);jtf.addActionListener(new MyFieldDealing();JPanel panel=new JPanel();panel.setLayout(new GridLayout(2,1);panel.add(jlabel);panel.add(
8、jtf);con.add(panel,BorderLayout.NORTH);edpl=new JEditorPane();edpl.setEditable(false);edpl.addHyperlinkListener(new MyHyperListener();con.add(new JScrollPane(edpl),BorderLayout.CENTER);setSize(600,800);setVisible(true);void getPage(String site)tryedpl.setPage(site); /在 edpl 中显示指定网页catch(IOException
9、e)JOptionPane.showMessageDialog(this,“连接错误“,“错误提示“,JOptionPane.ERROR_MESSAGE);class MyEnter implements ActionListener /处理输入网址时的回车操作public void actionPerformed(ActionEvent e)String string=jtf.getText();getPage(string);class MyHyperListener implements HyperlinkListener/处理网页中的超链接public void hyperlinkUpdate(HyperlinkEvent e)if(e.getEventType()=HyperlinkEvent.EventType.ACTIVATED)String string=e.getURL().toString();/获取超链接指向的地址getPage(string);/显示超链接指向的网页内容public static void main(String args)browser mybrowser=new browser();mybrowser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);