使用Java开发自动化检测域名是否可注册工具
对大量域名的注册情况进行自动化检测,细分开来讲就是多次对单个域名的注册情况的检测。而对单个域名的检测我们可以使用万网提供的接口(http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=xxx.yy),具体细节可以参考我以前写过的一篇文章:http://www.zifangsky.cn/2015/11/java中的get和post请求,使用万网接口判断域名是否已被注/
因此,在这篇文章里,我主要实现的是:(1)做了一个比较直观的图形化界面;(2)支持从外部导入字典文件进行自动化检测;(3)支持对多种后缀的检测,包括可以自定义后缀;(4)同时还实现了对探测结果的导出。
运行后的截图基本上是这样的:
界面和导入导出功能不用多说,实现起来很简单,而且代码中有详细注释,一看就懂。这里需要说明的是,为了让检测的状态在界面中实时显示出来(左边的正在检测以及右边的可注册和超时域名),我选择了在一个新的线程里执行文件读取,联网查询这类比较费时的工作,同时使用了SwingUtilities.invokeLater来通知事件分发线程及时更新界面。具体原理和基本实现可参考我写的这篇文章:http://www.zifangsky.cn/2015/12/java中事件分发线程(edt)与swingutilities-invokelater相关总结/
二 具体的代码实现
package view; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileSystemView; public class MainView extends JFrame implements ActionListener, MouseListener{ /** * 自动化批量检测域名是否已注册 * @author zifangsky * @blog http://www.zifangsky.cn * @date 2015-12-30 * @version v1.0.0 */ private static final long serialVersionUID = 1L; private GridBagLayout gridbag; private GridBagConstraints constraints; private JPanel mainJPanel,panel1,panel2,panel3,panel4,panel5,panel6,panel7; private JButton selectDic,beginCheck; // 选择字典,开始检测 private JLabel domainSuffix,custom,progress,result; //域名后缀,自定义后缀,探测进度,结果 private JFileChooser dicChooser; //字典选择 private JTextField customJTextField; //自定义 private JScrollPane progressPane,resultPane; //进度面板和结果面板 private JTextArea progressJtJTextArea,resultJTextArea; //同上 private JCheckBox[] suffixCheckBoxs = new JCheckBox[15]; //域名后缀多项选择框 private JMenuBar jMenuBar; private JMenu help; private JMenuItem author,contact,version,readme; private JPopupMenu outPutData; //导出数据 private JMenuItem availableDomains,timeOutDomains,allDomains; //可用域名,超时域名,全部域名 private Font menuFont = new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 14); //菜单字体 private Font contentFont = new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 16); //正文字体 private String dicName = "",currentDomain = ""; //字典名字,当前检测域名 private DomainsCheckThread myThread = null; //查询线程 private Runnable progressRunnable,resultRunnable,timedOutRunnable,endRunnable; //更新页面线程 public MainView(){ super("自动化域名检测"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setPreferredSize(new Dimension(1000, 650)); int frameWidth = this.getPreferredSize().width; //界面宽度 int frameHeight = this.getPreferredSize().height; //界面高度 setSize(frameWidth,frameHeight); setLocation((screenSize.width - frameWidth) / 2,(screenSize.height - frameHeight) / 2); //初始化 mainJPanel = new JPanel(); panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); panel5 = new JPanel(); panel6 = new JPanel(); panel7 = new JPanel(); selectDic = new JButton("导入字典文件"); beginCheck = new JButton("开始检测"); domainSuffix = new JLabel("域名后缀:"); custom = new JLabel("自定义(以英文空格分割,如:.club .win):"); progress = new JLabel("探测进度:"); result = new JLabel("探测结果:"); customJTextField = new JTextField("",70); progressPane = new JScrollPane(); resultPane = new JScrollPane(); progressJtJTextArea = new JTextArea(20, 20); resultJTextArea = new JTextArea(20, 20); suffixCheckBoxs[0] = new JCheckBox(".com"); suffixCheckBoxs[1] = new JCheckBox(".cn"); suffixCheckBoxs[2] = new JCheckBox(".com.cn"); suffixCheckBoxs[3] = new JCheckBox(".org"); suffixCheckBoxs[4] = new JCheckBox(".net"); suffixCheckBoxs[5] = new JCheckBox(".me"); suffixCheckBoxs[6] = new JCheckBox(".cc"); suffixCheckBoxs[7] = new JCheckBox(".xyz"); suffixCheckBoxs[8] = new JCheckBox(".top"); suffixCheckBoxs[9] = new JCheckBox(".xin"); suffixCheckBoxs[10] = new JCheckBox(".biz"); suffixCheckBoxs[11] = new JCheckBox(".tv"); suffixCheckBoxs[12] = new JCheckBox(".ren"); suffixCheckBoxs[13] = new JCheckBox(".wang"); suffixCheckBoxs[14] = new JCheckBox(".link"); suffixCheckBoxs[0].setSelected(true); //第一个默认选中 //布局 gridbag = new GridBagLayout(); constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.BOTH; mainJPanel.setLayout(gridbag); constraints.gridwidth = 0; //该方法是设置组件水平所占用的格子数,如果为0,就说明该组件是该行的最后一个 constraints.gridheight = 1; constraints.weightx = 1; //该方法设置组件水平的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间 constraints.weighty = 0; //该方法设置组件垂直的拉伸幅度,如果为0就说明不拉伸,不为0就随着窗口增大进行拉伸,0到1之间 gridbag.setConstraints(panel1, constraints); mainJPanel.add(panel1); constraints.gridheight = 2; gridbag.setConstraints(panel2, constraints); mainJPanel.add(panel2); constraints.gridheight = 1; gridbag.setConstraints(panel3, constraints); mainJPanel.add(panel3); gridbag.setConstraints(panel4, constraints); mainJPanel.add(panel4); constraints.weightx = 1; constraints.weighty = 1; gridbag.setConstraints(panel5, constraints); mainJPanel.add(panel5); panel1.setLayout(new FlowLayout(FlowLayout.CENTER)); selectDic.setFont(contentFont); panel1.add(selectDic); panel2.setLayout(new FlowLayout(FlowLayout.LEFT)); domainSuffix.setFont(contentFont); panel2.add(domainSuffix); for(int i=0;i suffixSet = new LinkedHashSet(); for(int i=0;i1;超时-->2;所有-->3 * @return null * */ private void resultOutPut(Pattern pattern,int state){ String[] data = resultJTextArea.getText().split("\n"); Date date = new Date(); Format format = new SimpleDateFormat("HHmmss"); String fileName = ""; if(state == 1) fileName = "可注册域名导出列表" + format.format(date) + ".txt"; else if(state == 2) fileName = "超时域名导出列表" + format.format(date) + ".txt"; else if(state == 3) fileName = "所有结果域名导出列表" + format.format(date) + ".txt"; try { BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName))); Matcher matcher = null; for(String temp : data){ matcher = pattern.matcher(temp); if(matcher.find()){ writer.write(matcher.group(1)); writer.newLine(); writer.flush(); } } writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 查询线程,用的万网的接口 * */ class DomainsCheckThread implements Runnable{ private Set suffixSet = new LinkedHashSet (); public DomainsCheckThread(Set suffixSet) { this.suffixSet = suffixSet; } public void run() { //读字典 try { BufferedReader reader = new BufferedReader(new FileReader(new File(dicName))); String line = ""; while((line = reader.readLine()) != null){ if(!"".equals(line.trim())){ Iterator iterator = suffixSet.iterator(); while(iterator.hasNext()){ currentDomain = line.trim() + iterator.next(); SwingUtilities.invokeLater(progressRunnable); //更新状态 checkDomain(currentDomain); //开始查询 try { Thread.sleep(1000); //单线程,并且每次查询完毕暂停1秒 } catch (InterruptedException e) { e.printStackTrace(); } } } } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //任务结束,参数初始化 dicName = ""; currentDomain = ""; myThread = null; SwingUtilities.invokeLater(endRunnable); //结束通知 } /** * 对单个域名向万网的接口发起请求,检测注册情况 * @param domain 域名 * @return null * */ public void checkDomain(String domain){ try { URL url = new URL("http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + domain); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(3000); //毫秒 connection.setReadTimeout(3000); if(connection.getResponseCode() == 200){ InputStream inputStream = new BufferedInputStream(connection.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = ""; String reg = " (.*?) "; while((line = reader.readLine()) != null){ if(line.matches(reg)){ //211表示不可用 String state = line.substring(10, 13); if(!"211".equals(state)){ //该域名未被使用,更新状态 SwingUtilities.invokeLater(resultRunnable); } } } reader.close(); inputStream.close(); } connection.disconnect(); } catch (IOException e) { //超时,更新状态 SwingUtilities.invokeLater(timedOutRunnable); } } } /** * 鼠标点击 * */ public void mouseClicked(MouseEvent e) { //鼠标右键点击探测结果面板时,弹出数据导出菜单 if(e.getButton() ==MouseEvent.BUTTON3){ outPutData.show(resultJTextArea, e.getX(), e.getY()); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }
三 效果测试
注:其实这个软件仅仅只是一个初始版本,还很不完善。因为万网的接口是免费的原因,因此不仅不能使用多线程,而且我还人为的在每次请求后暂停了一秒钟。后期我根据情况或许会将这个软件升级成多线程版的,尽请期待
附:
源代码和软件的下载链接:链接:http://pan.baidu.com/s/1eQNvr8I 密码:rw4h
为方便米农,我还用代码生成了几个实用的字典:
字典文件的下载链接:http://pan.baidu.com/s/1boplCEN
上一篇: java基础-类变量,类方法