⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sitesubmit.java

📁 < 网络机器人java编程指南>>的配套源程序
💻 JAVA
字号:
import java.io.*;import java.awt.*;import javax.swing.*;import com.heaton.bot.*;/** * Example program from Chapter 1 * Programming Spiders, Bots and Aggregators in Java * Copyright 2001 by Jeff Heaton * * * This is an example program that submit your site * to five popular search engines. This shows how to * use the HTMLForm object. * * @author Jeff Heaton * @version 1.0 */public class SiteSubmit extends javax.swing.JFrame {  /**   * A list of search engines to submit to.   */  protected Site _list[] = {    new Site("Lycos(tm)", // engine name             "http://home.lycos.com/addasite.html",             "query",// url             "email",// email             null,// keys             null,// comments             null,// user name             null),// submit    new Site("HotBot(tm)", // engine name             "http://hotbot.lycos.com/addurl.asp",             "newurl",// url             "email",// email             null,// keys             null,// comments             null,// user name             null),// submit    new Site("Direct Hit(tm)",             "http://www.directhit.com/util/addurl.html",             "URL",// url             "email",// email             "keys",// keys             null,// comments             null,// user name             null),// submit    new Site("Northern Light(tm)",             "http://www.northernlight.com/docs/regurl_help.html",             "page",// url             "EMAIL",// email             null,// keys             null,// comments             "contact",// user name             null),// submit    new Site("Google(tm)", // engine name             "http://www.google.com/addurl.html",// post             "q",// url             null,// email             null,// keys             "dq",// comments             null,// user name             null)// submit  };  /**   * The constructor. Setup all the controls.   */  public SiteSubmit()  {    //{{INIT_CONTROLS    getContentPane().setLayout(null);    setSize(405,370);    setVisible(false);    JLabel1.setVerticalTextPosition(      javax.swing.SwingConstants.TOP);    JLabel1.setVerticalAlignment(      javax.swing.SwingConstants.TOP);    JLabel1.setText(      "Please enter the site that you would like to submit.");    getContentPane().add(JLabel1);    JLabel1.setBounds(12,0,360,24);    JLabel2.setText("URL:");    getContentPane().add(JLabel2);    JLabel2.setBounds(12,36,36,12);    JLabel3.setText("Your E-Mail Address:");    getContentPane().add(JLabel3);    JLabel3.setBounds(12,72,120,12);    JLabel4.setText("Key Words:");    getContentPane().add(JLabel4);    JLabel4.setBounds(12,108,108,12);    JLabel5.setText("Comments:");    getContentPane().add(JLabel5);    JLabel5.setBounds(12,144,96,12);    JLabel6.setText("Your name:");    getContentPane().add(JLabel6);    JLabel6.setBounds(12,180,108,12);    getContentPane().add(_url);    _url.setBounds(144,36,252,24);    getContentPane().add(_email);    _email.setBounds(144,72,252,24);    getContentPane().add(_keys);    _keys.setBounds(144,108,252,24);    getContentPane().add(_keys);    getContentPane().add(_comments);    _comments.setBounds(144,144,252,24);    getContentPane().add(_username);    _username.setBounds(144,180,252,24);    JButton1.setText("Submit");    JButton1.setActionCommand("Submit");    getContentPane().add(JButton1);    JButton1.setBounds(48,216,84,24);    JButton2.setText("Quit");    JButton2.setActionCommand("Quit");    getContentPane().add(JButton2);    JButton2.setBounds(264,216,84,24);    JScrollPane1.setOpaque(true);    getContentPane().add(JScrollPane1);    JScrollPane1.setBounds(12,252,384,108);    JScrollPane1.getViewport().add(_output);    _output.setBounds(0,0,381,105);    //}}    //{{INIT_MENUS    //}}    //{{REGISTER_LISTENERS    SymAction lSymAction = new SymAction();    JButton2.addActionListener(lSymAction);    JButton1.addActionListener(lSymAction);    //}}    setLocation(32,32);  }  /**   * Generated by Visual Cafe.   *   * @param b True/false to set the visiblity of the window.   */  public void setVisible(boolean b)  {    if ( b )      setLocation(50, 50);    super.setVisible(b);  }  /**   * The main entry point of the program.   *   * @param args No program arguments are used.   */  static public void main(String args[])  {    (new SiteSubmit()).setVisible(true);  }  /**   * Generated by Visual Cafe to setup all   * notifications.   */  public void addNotify()  {    // Record the size of the window prior to    // calling parents addNotify.    Dimension size = getSize();    super.addNotify();    if ( frameSizeAdjusted )      return;    frameSizeAdjusted = true;    // Adjust size of frame according to the insets and menu bar    Insets insets = getInsets();    javax.swing.JMenuBar menuBar = getRootPane().getJMenuBar();    int menuBarHeight = 0;    if ( menuBar != null )      menuBarHeight = menuBar.getPreferredSize().height;    setSize(insets.left +            insets.right +            size.width, insets.top +            insets.bottom + size.height +            menuBarHeight);  }  // Used by addNotify  boolean frameSizeAdjusted = false;  //{{DECLARE_CONTROLS  javax.swing.JLabel JLabel1  = new javax.swing.JLabel();  javax.swing.JLabel JLabel2  = new javax.swing.JLabel();  javax.swing.JLabel JLabel3  = new javax.swing.JLabel();  javax.swing.JLabel JLabel4  = new javax.swing.JLabel();  javax.swing.JLabel JLabel5  = new javax.swing.JLabel();  javax.swing.JLabel JLabel6  = new javax.swing.JLabel();  javax.swing.JTextField _url  = new javax.swing.JTextField();  javax.swing.JTextField _email  = new javax.swing.JTextField();  javax.swing.JTextField _keys  = new javax.swing.JTextField();  javax.swing.JTextField _comments  = new javax.swing.JTextField();  javax.swing.JTextField _username  = new javax.swing.JTextField();  javax.swing.JButton JButton1  = new javax.swing.JButton();  javax.swing.JButton JButton2  = new javax.swing.JButton();  javax.swing.JScrollPane JScrollPane1  = new javax.swing.JScrollPane();  javax.swing.JTextArea _output  = new javax.swing.JTextArea();  //}}  //{{DECLARE_MENUS  //}}  /**   * This internal class is used to hold information   * about an individual search engine.   *   * @author Jeff Heaton   */  class Site {    /**     * The constructor. Used to create a site.     *     * @param name The name of the person submitting.     * @param post The page that contains the submit form.     * @param url Field name: the URL being submitted     * @param email Field name: the email address of the     * person submitting.     * @param keys Field name: keywords for this site     * @param comments Field name: comments for this site     * @param username Field name: who is submitting     * @param submit Field name: the name of the submit button     */    public Site(               String name,               String post,               String url,               String email,               String keys,               String comments,               String username,               String submit )    {      _name = name;      _post = post;      _url = url;      _email = email;      _keys = keys;      _comments = comments;      _username = username;      _submit = submit;    }    /**     * The name of this search engine.     */    public String _name;    /**     * The page used to post a new site from.     * This is the page that contains the form.     */    public String _post;    /**     * The name of the <input> tag used to     * specify the URL to be submitted.     */    public String _url;    /**     * The name of the <input> tag used to     * specify the email address of the     * person submitting.     */    public String _email;    /**     * The name of the <input> tag used to     * specify the keywords of the site.     */    public String _keys;    /**     * The name of the <input> tag used to     * specify comments about the site.     */    public String _comments;    /**     * The name of the <input> tag used to     * specify the user name of the person     * submitting the site.     */    public String _username;    /**     * The name of the <input> tag used to     * specify the name of the submit control.     */    public String _submit;  }  /**   * This internal class is used by Visual Cafe   * generated code to dispatch events.   */  class SymAction implements java.awt.event.ActionListener {    public void actionPerformed(java.awt.event.ActionEvent event)    {      Object object = event.getSource();      if ( object == JButton2 )        JButton2_actionPerformed(event);      else if ( object == JButton1 )        JButton1_actionPerformed(event);    }  }  /**   * Quit the program.   *   * @param event The button event.   */  void JButton2_actionPerformed(java.awt.event.ActionEvent event)  {    System.exit(0);  }  /**   * Called to write a line of text to the edit control.   * This lets the user review the outcome.   *   * @param text   */  protected void log(String text)  {    _output.insert(text + "\n",_output.getText().length());  }  /**   * Main function that does the submit. Triggered   * by the submit button being clicked.   *   * @param event The button event.   */  void JButton1_actionPerformed(java.awt.event.ActionEvent event)  {    FileOutputStream fw = null;    PrintStream ps = null;    log("-------------------------------");    log("Submit: " + _url.getText() );    try {      fw = new FileOutputStream("result.log",false);      ps = new PrintStream(fw);    } catch ( Exception e ) {      log("Can't write to log file: " + e );    }    for ( int i=0;i<_list.length;i++ ) {      try {        // download and parse the submit page form        // the search engine.        HTMLPage page = new HTMLPage(new HTTPSocket());        page.open(_list[i]._post,null);        // Some of these pages have more than one        // form. Find the correct form.        // The one that contains a control with a        // name matching the one that we plan to        // copy the url into will signify the correct        // form.        int j=0;        HTMLForm found = null;        while ( j<page.getForms().size() ) {          HTMLForm form = (HTMLForm)page.getForms().elementAt(j);          if ( form.get( _list[i]._url ) !=null ) {            found = form;            break;          }          j++;        }        if ( found==null ) {          log("**Submit to: " +  _list[i]._name + " Failed: No form" );          continue;        }        // fill in all form elements that are required        // for this search engine.        if ( _list[i]._url!=null )          found.set(_list[i]._url,_url.getText());        if ( _list[i]._keys!=null )          found.set(_list[i]._keys,_keys.getText());        if ( _list[i]._email!=null )          found.set(_list[i]._email,_email.getText());        if ( _list[i]._comments!=null )          found.set(_list[i]._comments,_comments.getText());        if ( _list[i]._username!=null )          found.set(_list[i]._username,_username.getText());        // now attempt to post(or get) the form        page.post(found);        if ( ps!=null ) {          ps.println("******************************************");          ps.println(page.getHTTP().getBody());        }        log("Submit to: " +  _list[i]._name );      } catch ( Exception e ) {        log("**Submit to: " +  _list[i]._name + "(" + _list[i]._post +")" + " Failed:" + e );      }    }    try {      ps.close();      fw.close();    } catch ( Exception e ) {    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -