使用ViewURLCookie分析Cookie

 ViewURLCookie:

通过使用ViewURLCookie这个新例程,我们就可以分析给定URL返回的Cookie;

效果如下:

 

实现代码如下:

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.table.*;
  4. import com.heaton.bot.*;
  5. /**
  6.  * This application displays a dialog box that
  7.  * allows the user to specify any URL. This URL
  8.  * is requested, using the Bot package, and displayed
  9.  * in the dialog box. Both the body and HTTP headers
  10.  * are displayed, as well aso cookie information.
  11.  * This example is very similar to the ViewURL example
  12.  * from Chapter 2, except this version works with cookies.
  13.  */
  14. public class ViewURLCookie extends javax.swing.JFrame {
  15.   /**
  16.    * The HTTP connection used by this application.
  17.    */
  18.   HTTP _http;
  19.   /**
  20.    * The constructor.  This method sets up all the
  21.    * components needed by this class.
  22.    * A new HTTPSocket object is also constructed
  23.    * to manange the connection.
  24.    */
  25.   public ViewURLCookie()
  26.   {
  27.     _http = new HTTPSocket();
  28.     _http.setUseCookies(true,true);
  29.     
  30.     /*
  31.      * 初始化控制界面
  32.      */
  33.     setTitle("View URL");
  34.     getContentPane().setLayout(null);
  35.     setSize(490,462);
  36.     setVisible(false);
  37.     _pane2.setOpaque(true);
  38.     getContentPane().add(_pane2);
  39.     _pane2.setBounds(12,312,456,144);
  40.     _body.setEditable(false);
  41.     _pane2.getViewport().add(_body);
  42.     _body.setBounds(0,0,453,141);
  43.     _pane1.setOpaque(true);
  44.     getContentPane().add(_pane1);
  45.     _pane1.setBounds(12,72,456,72);
  46.     _pane1.getViewport().add(_headers);
  47.     _headers.setBounds(0,0,453,0);
  48.     _label3.setText("Body");
  49.     getContentPane().add(_label3);
  50.     _label3.setBounds(12,288,456,12);
  51.     _label1.setText("URL:");
  52.     getContentPane().add(_label1);
  53.     _label1.setBounds(12,12,36,24);
  54.     
  55.     _url.setText("http://www.jeffheaton.com");
  56.     getContentPane().add(_url);
  57.     _url.setBounds(48,12,348,24);
  58.     
  59.     _go.setText("Go");
  60.     _go.setActionCommand("Go");
  61.     getContentPane().add(_go);
  62.     _go.setBounds(408,12,60,24);
  63.     
  64.     _label2.setText("HTTP Headers");
  65.     getContentPane().add(_label2);
  66.     _label2.setBounds(12,48,384,12);
  67.     JLabel1.setText("Cookies");
  68.     getContentPane().add(JLabel1);
  69.     JLabel1.setBounds(12,156,456,12);
  70.     
  71.     JScrollPane1.setOpaque(true);
  72.     getContentPane().add(JScrollPane1);
  73.     JScrollPane1.setBounds(12,180,456,96);
  74.     JScrollPane1.getViewport().add(_cookies);
  75.     _cookies.setBounds(0,0,453,0);
  76.     
  77.     /*
  78.      * REGISTER_LISTENERS
  79.      */
  80.     
  81.     SymWindow aSymWindow = new SymWindow();
  82.     this.addWindowListener(aSymWindow);
  83.     
  84.     SymAction lSymAction = new SymAction();
  85.     _go.addActionListener(lSymAction);
  86.   
  87.     setLocation(32,32);
  88.   }
  89.   /**
  90.    * Set the visibility of this window.
  91.    *
  92.    * @param b true for visible, false for invisible
  93.    */
  94.   public void setVisible(boolean b)
  95.   {
  96.     if ( b )
  97.       setLocation(5050);
  98.     super.setVisible(b);
  99.   }
  100.   /**
  101.    * The entry point for this application.
  102.    *
  103.    * @param args Arguments are not used by this program.
  104.    * Required for proper main signature.
  105.    */
  106.   static public void main(String args[])
  107.   {
  108.     (new ViewURLCookie()).setVisible(true);
  109.   }
  110.   /**
  111.    * Called to add notification handlers.
  112.    */
  113.   public void addNotify()
  114.   {
  115.     // Record the size of the window prior to
  116.     // calling parents addNotify.
  117.     Dimension size = getSize();
  118.     super.addNotify();
  119.     if ( frameSizeAdjusted )
  120.       return;
  121.     frameSizeAdjusted = true;
  122.     // Adjust size of frame according to the insets
  123.     Insets insets = getInsets();
  124.     setSize(insets.left +
  125.             insets.right +
  126.             size.width,
  127.             insets.top +
  128.             insets.bottom +
  129.             size.height);
  130.   }
  131.   /**
  132.    * DECLARE_CONTROLS
  133.    * 申明控件
  134.    */
  135.   boolean frameSizeAdjusted = false;
  136.   javax.swing.JScrollPane _pane2 = new javax.swing.JScrollPane();
  137.   javax.swing.JTextArea _body = new javax.swing.JTextArea();
  138.   javax.swing.JScrollPane _pane1 = new javax.swing.JScrollPane();
  139.   javax.swing.JTable _headers = new javax.swing.JTable();
  140.   javax.swing.JLabel _label3 = new javax.swing.JLabel();
  141.   javax.swing.JLabel _label1 = new javax.swing.JLabel();
  142.   javax.swing.JTextField _url = new javax.swing.JTextField();
  143.   javax.swing.JButton _go = new javax.swing.JButton();
  144.   javax.swing.JLabel _label2 = new javax.swing.JLabel();
  145.   javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
  146.   javax.swing.JScrollPane JScrollPane1 = new javax.swing.JScrollPane();
  147.   javax.swing.JTable _cookies = new javax.swing.JTable();
  148.   //}}
  149.   /**
  150.    * Class created 
  151.    * what mean???
  152.    */
  153.   class SymWindow extends java.awt.event.WindowAdapter {
  154.     public void windowClosed(java.awt.event.WindowEvent event)
  155.     {
  156.       Object object = event.getSource();
  157.       if ( object == ViewURLCookie.this )
  158.         ViewURLCookie_windowClosed(event);
  159.     }
  160.     public void windowClosing(java.awt.event.WindowEvent event)
  161.     {
  162.       Object object = event.getSource();
  163.       if ( object == ViewURLCookie.this )
  164.         ViewURLCookie_WindowClosing(event);
  165.     }
  166.   }
  167.   /**
  168.    * Called when the window closes.
  169.    */
  170.   void ViewURLCookie_WindowClosing(java.awt.event.WindowEvent event)
  171.   {
  172.     // Hide the Frame
  173.     setVisible(false);
  174.  /*
  175.  * Free the system resources
  176.  * 释放资源
  177.  */
  178.     dispose();
  179.   }
  180.   /**
  181.    * 创建用于侦听的类
  182.    */
  183.   class SymAction implements java.awt.event.ActionListener {
  184.     public void actionPerformed(java.awt.event.ActionEvent event)
  185.     {
  186.       Object object = event.getSource();
  187.       if ( object == _go )
  188.         Go_actionPerformed(event);
  189.     }
  190.   }
  191.   /**
  192.    * Called when the GO button is clicked.
  193.    */
  194.   void Go_actionPerformed(java.awt.event.ActionEvent event)
  195.   {
  196.     try {
  197.       _http.send(_url.getText(),null);
  198.       _body.setText(_http.getBody());
  199.       _url.setText(_http.getURL());
  200.       // handle the headers
  201.       TableModel dataModelHeader = new AbstractTableModel()
  202.       {
  203.         public int getColumnCount() 
  204.         { 
  205.             return 2;
  206.         }
  207.         public int getRowCount() 
  208.         { 
  209.             return _http.getServerHeaders().length();
  210.         }
  211.         
  212.         public String getColumnName(int columnIndex)
  213.         {
  214.           switch ( columnIndex ) 
  215.           {
  216.             case 0:return "HTTP Header";
  217.             case 1:return "Value";
  218.           }
  219.           return "";
  220.         }
  221.         
  222.         public Object getValueAt(int row, int col)
  223.         {
  224.           if ( col==0 )
  225.             return _http.getServerHeaders().get(row).getName();
  226.           else
  227.             return _http.getServerHeaders().get(row).getValue();
  228.         }
  229.       };
  230.       _headers.setModel(dataModelHeader);
  231.       /*
  232.        * Sets the data model for this table to newModel and 
  233.        * registers with it for listener notifications from the
  234.          new data model. 
  235.          See Also:
  236.          getModel
  237.          Parameters:
  238.          dataModel the new data source for this table
  239.          Throws:
  240.          IllegalArgumentException if newModel is null
  241.        */
  242.       _headers.sizeColumnsToFit(0);
  243.       // handle the cookies
  244.       TableModel dataModelCookie = new AbstractTableModel()
  245.       {
  246.         public int getColumnCount() 
  247.         { 
  248.             return 2;
  249.         }
  250.         public int getRowCount() 
  251.         { 
  252.             return _http.getCookies().length();
  253.         }
  254.         public String getColumnName(int columnIndex)
  255.         {
  256.           switch ( columnIndex ) 
  257.           {
  258.              case 0:return "Cookie Name";
  259.              case 1:return "Value";
  260.           }
  261.           return "";
  262.         }
  263.         public Object getValueAt(int row, int col)
  264.         {
  265.           if ( col==0 )
  266.             return _http.getCookies().get(row).getName();
  267.           else
  268.             return _http.getCookies().get(row).toString();
  269.         }
  270.       };
  271.       _cookies.setModel(dataModelCookie);
  272.       _cookies.sizeColumnsToFit(0);
  273.     } catch ( Exception e ) {
  274.       _body.setText(e.toString());
  275.     }
  276.   }
  277.   /**
  278.    * Called once the window closes.
  279.    * what for???
  280.    */
  281.   void ViewURLCookie_windowClosed(java.awt.event.WindowEvent event)
  282.   {
  283.     System.exit(0);
  284.   }
  285. }

因为要进行调试一下,进行底层的分析。所以就……

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值