网络编程---实验2 查找Internet地址和用URL检索数据

查找Internet地址和用URL检索数据

1.实验内容

1)InetAddress类的用法

2)URL类的用法

2.实验内容

1.完成以下程序,写出主要代码,并画出程序的主要流程图。

第一题:域名与IP地址检索

在图形界面的文本框中输入主机名(域名),在下面的文本区中显示其IP地址信息;如果输入IP地址,则显示其主机名。

 

要求:

  1. 如果在文本框中输入的是主机名,要求查询出其对应的所有IP地址。
  2. 编写合适的方法,检测用户输入的是域名还是IP地址。
  3. 当用户输入的信息无法被转换成正确的域名和IP地址时,需要给出适当的提示信息。

2. 完成以下程序,写出主要代码,并画出程序的主要流程图。

第二题:编写一个简易的浏览器程序

参考界面如下:

 

要求:

  1. 能够检测用户输入的URL地址是否合法:如用户只是输入了www.baidu.com,能够将其转换成http://www.baidu.com
  2. 中英文域名转换:如果用户输入了中文“百度”,能够将其转换为http://www.baidu.com。(要求至少能处理3-5个中文域名的转换)
  3. 能够处理所显示的URL资源中的超链接。
  4. 如果单击超链接,请将文本框中的内容换成对应的超链接的地址信息。

3.参考代码

3.1第一问

package shiyan2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.*;
/*
*
* 输入一个域名可以解析出对应的ip地址的信息
* JFrame窗口类
*ActionListener设置点击事件的
* */
public class Address_Resolution extends JFrame implements ActionListener{
    JLabel labelOfAddress;//提示要输入的信息
    JTextField textFldOfURLAddress;//接收输入的信息
    JButton okBtn;//确定的按钮
    JTextArea textFldOfResult;//显示结果

    //构造方法
    public Address_Resolution() {
        this.setTitle("域名解析");
        //lable设置
        labelOfAddress = new JLabel("输入主机名或者IP地址:");
        textFldOfURLAddress = new JTextField(20);
        okBtn = new JButton("确定");

        //设置事件监听器。将this传入,相当于setXxxListener
        okBtn.addActionListener(this);
        textFldOfURLAddress.addActionListener(this);

        //设置第一行的显示的内容
        JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        jp1.add(labelOfAddress);
        jp1.add(textFldOfURLAddress);
        jp1.add(okBtn);

        //BorderLayout.NORTH表示沿水平方向排列。
        this.add(jp1,BorderLayout.NORTH);

        //显示结果的信息
        textFldOfResult = new JTextArea();
        //只显示不能编辑
        textFldOfResult.setEditable(false);
        this.add(textFldOfResult);
        //设置为位置和宽度大小
        this.setLocation(460,100);
        this.setSize(600,400);
        this.setVisible(true);
        //关闭的操作
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //重写的事件监听的方法,用于设置按钮的点击事件,当按钮发生变化了就会
    /*
    * 点击确定按钮或者是回车都会触发查询的事件
    * getSource()方法返回的是对象的本身,而不是对象的引用。
    * */
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == okBtn || e.getSource() == textFldOfURLAddress) {
            //获取用户输入的数据
            String dnOrIp = textFldOfURLAddress.getText().trim();
            try {
                //不是数字的ip地址的时候,解析域名
                if (!isIP(dnOrIp)) {
                    /*
                    * getAllByName,获取该hostname下的所有的ip的信息
                    *getHostAddress获取ip的信息
                    * */

                    InetAddress[] allByName = InetAddress.getAllByName(dnOrIp);
                    textFldOfResult.setText(dnOrIp + "的IP地址为:\n");
                    //读取所有的ip的信息
                    for (InetAddress net:allByName) {
                        textFldOfResult.append(net.getHostAddress() + "\n");
                    }
                }
                //解析ip,输出域名。貌似没有效果
                else {
                    InetAddress address = InetAddress.getByName(dnOrIp);
                    textFldOfResult.setText(dnOrIp + "的域名为:\n");
                    textFldOfResult.append(address.getHostName());

                }

            }

            catch (UnknownHostException e1) {
                System.out.println("出错了:"+e1.getMessage()+"不正确");
                textFldOfResult.setText("出错了:"+e1.getMessage()+"不正确");
            }
            catch (IOException x){
            }

        }else {
            textFldOfResult.setText("您输入的数据不合法,请检查后重新数据!");
        }
    }


    /*
    *判断输入的是否是ip地址
    * 简介:根据输入的.进行分割4组
    *
    * */
    public static boolean isIP(String ip) {
        //记录是不是4组
      try{
          int count = 0;
          String[] temp = ip.split("\\.");
          for (int i = 0; i < temp.length; i++) {
              //isNumeric判断是不是数字的
              if(isNumeric(temp[i]) && 0<Integer.parseInt(temp[i]) && Integer.parseInt(temp[i])<256) {
                  count++;
              }
          }
          if(count == 4) {
              return true;
          } else {
              return false;
          }
      }
      catch (Exception e){
          return false;
      }
    }

    /*
    * 判断字符串是否是数字
    * 判断依据:根据强制转换出来的数据是不是发生异常信息来判断是不是发生异常的信息
    * BigDecimal是一种数值类型,它可以用来表示非常大的数字
    * */
    public static boolean isNumeric(String str) {
        try {
            new BigDecimal(str).toString();
        }
        //异常 说明包含非数字
        catch (Exception e) {
            return false;
        }
        return true;
    }

    /*
    * 测试程序
    * */
    public static void main(String[] args) {
        new Address_Resolution();
    }
}

效果图:

 

3.2第二问

参考代码

/*
* 输入网络地址显示内容
* */
public class InternetAndURL extends JFrame implements ActionListener,Runnable, HyperlinkListener {
    JEditorPane editPane;//网络资源文本区
    JScrollPane jScrollPane;
    JTextField textFldOfURLAddress;//显示网络地址的文本框
    JLabel lableOfAddress;
    JButton okBtn;
    URL url;
    Thread threadURL;
    byte[] b = new byte[118];

    public InternetAndURL() {
        lableOfAddress = new JLabel("请输入网址:");
        textFldOfURLAddress = new JTextField(40);
        okBtn = new JButton("确定");

        okBtn.addActionListener(this);
        textFldOfURLAddress.addActionListener(this);

        JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        jp1.add(lableOfAddress);
        jp1.add(textFldOfURLAddress);
        jp1.add(okBtn);

        this.add(jp1,BorderLayout.NORTH);

        editPane = new JEditorPane();
        editPane.setSize(12,12);
        editPane.setEditable(false);

        editPane.addHyperlinkListener(this);
        jScrollPane = new JScrollPane(editPane);
        this.add(jScrollPane);

        this.setLocation(300,200);
        this.setSize(600,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        threadURL = new Thread(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==okBtn || e.getSource()==textFldOfURLAddress) {
            if (!threadURL.isAlive()) {
                threadURL = new Thread(this);
                threadURL.start();
            }
        }
    }

    @Override
    public void run() {
        try {
            String[] Cyuming = {"www.sdu.edu.cn","www.baidu.com","www.sohu.com","www.taobao.com","www.bilibili.com"};
            String[] Eyuming = {"山东大学","百度","搜狐","淘宝","B站"};
            String str = textFldOfURLAddress.getText().trim();
            //中英文域名转换
            for (int i = 0; i < 5; i++) {
                if (Eyuming[i].equals(str)) {
                    str = Cyuming[i];
                }
            }
            //加协议
            if (!str.startsWith("http://")){
                str = "http://" + str;
            }
            url = new URL(str);

            editPane.setPage(url);
            textFldOfURLAddress.setText(url.toString());
        } catch (MalformedURLException e) {
        } catch (IOException e) {

        }
    }

    @Override
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {

            try {
                URL url1 = e.getURL();
                editPane.setPage(url1);
                textFldOfURLAddress.setText(url1.toString());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        InternetAndURL nw = new InternetAndURL();
        nw.setVisible(true);
    }
}

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简单点了

谢谢大佬

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值