主题:SWING实现新浪微博客户端(2)提取关注好友信息

思路:
当用户登录完成后,(登录问题请参见,SWING实现新浪微博客户端(1)自动登录功能 )
1,模拟用户动作自动进入关注好友页面
2,根据关注好友页面结构提取关注好友的用户信息
3,将用户信息发送到后台,(例子中是在CONSOL中输出)

一,分析URL地址结构通过分析得知关注用户的URL地址为 http://t.sina.com.cn/2023096077/follow
而登录成功后的地址为

主题:SWING实现新浪微博客户端(2)提取关注好友信息_第1张图片

二,根据关注好友页面结构提取关注好友的用户信息

主题:SWING实现新浪微博客户端(2)提取关注好友信息_第2张图片



三,将用户信息发送到后台,(例子中是在CONSOL中输出)

主题:SWING实现新浪微博客户端(2)提取关注好友信息_第3张图片

Java代码   收藏代码
  1. import java.awt.BorderLayout;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.net.URL;  
  9. import java.util.Properties;  
  10.   
  11. import javax.swing.BorderFactory;  
  12.   
  13. import javax.swing.JFrame;  
  14. import javax.swing.JPanel;  
  15. import javax.swing.SwingUtilities;  
  16.   
  17.   
  18. import chrriis.common.UIUtils;  
  19. import chrriis.dj.nativeswing.swtimpl.NativeInterface;  
  20. import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;  
  21. import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;  
  22. import chrriis.dj.nativeswing.swtimpl.components.WebBrowserCommandEvent;  
  23.   
  24. import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;  
  25.   
  26.   
  27.   
  28.   
  29.   
  30. public class SSOSina extends JPanel {  
  31.   
  32.   protected static final String LS = System.getProperty("line.separator");  
  33.    private static final  String  loginUrl="http://t.sina.com.cn";  
  34.      
  35.    private static  Properties  userProperties=new Properties();  
  36.       
  37.   public SSOSina() {  
  38.     super(new BorderLayout());  
  39.       
  40.     InputStream in = this.getClass().getResourceAsStream("userProperties.properties");  
  41.     if (in == null) {  
  42.         in = Thread.currentThread().getContextClassLoader().getResourceAsStream("userProperties.properties");  
  43.         if (in == null) {  
  44.             in = this.getClass().getClassLoader().getResourceAsStream("userProperties.properties");  
  45.         }  
  46.     }  
  47.       
  48.     try {  
  49.         userProperties.load(in);  
  50.     } catch (IOException e1) {  
  51.         e1.printStackTrace();  
  52.     }  
  53.       
  54.     JPanel webBrowserPanel = new JPanel(new BorderLayout());  
  55.     webBrowserPanel.setBorder(BorderFactory.createTitledBorder("DJNative JAVA浏览器实现 实现新浪微博自动登录"));  
  56.     final JWebBrowser webBrowser = new JWebBrowser();  
  57.     webBrowser.setBarsVisible(true);  
  58.     webBrowser.setDefaultPopupMenuRegistered(true);  
  59.     webBrowser.setStatusBarVisible(true);  
  60.      
  61.     webBrowser.navigate(loginUrl);  
  62.     //单点登录自动提交监听器  
  63.     class SaveUserListener extends WebBrowserAdapter{  
  64.            @Override  
  65.            public void commandReceived(WebBrowserCommandEvent e) {  
  66.              String command = e.getCommand();  
  67.              Object[] parameters = e.getParameters();  
  68.              if("print".equals(command)) {  
  69.                String html = (String)parameters[0] ;  
  70.                System.out.println(html);  
  71.              }  
  72.              if("saveuser".equals(command)) {  
  73.                 String loginname = (String)parameters[0] ;  
  74.                 String password = (String)parameters[1] ;  
  75.              userProperties.setProperty("loginname", loginname);  
  76.              userProperties.setProperty("password", password);  
  77.                 try {  
  78.                     String runningURL = (new URL(SaveUserListener.class  
  79.                             .getProtectionDomain().getCodeSource().getLocation(),  
  80.                             ".")).openConnection().getPermission().getName();  
  81.                     userProperties.save(new   FileOutputStream(new File(runningURL+"userProperties.properties")),"changed");  
  82.                       
  83.                 } catch (FileNotFoundException e1) {  
  84.                     e1.printStackTrace();  
  85.                 } catch (IOException e1) {  
  86.                   
  87.                     e1.printStackTrace();  
  88.                 }       
  89.                 
  90.              }  
  91.            }  
  92.              
  93.     }  
  94.       
  95.       
  96.     class ReLoginListener extends WebBrowserAdapter{  
  97.               public void locationChanged(WebBrowserNavigationEvent e) {  
  98.                 if (e.getNewResourceLocation().equals("http://t.sina.com.cn")){  
  99.                       userProperties.setProperty("loginname""");  
  100.                        userProperties.setProperty("password""");  
  101.                     try {  
  102.                         String runningURL = (new URL(SaveUserListener.class  
  103.                                 .getProtectionDomain().getCodeSource().getLocation(),  
  104.                                 ".")).openConnection().getPermission().getName();  
  105.                         userProperties.save(new   FileOutputStream(new File(runningURL+"jdsclient_init.properties")),"changed");  
  106.                           
  107.                     } catch (FileNotFoundException e1) {  
  108.                         e1.printStackTrace();  
  109.                     } catch (IOException e1) {  
  110.                       
  111.                         e1.printStackTrace();  
  112.                     }       
  113.                     
  114.                    
  115.                          
  116.                 String script="function saveuser(){" +LS+  
  117.                  "sendNSCommand('saveuser',document.getElementById('loginname').value,document.getElementById('password').value);"+LS+  
  118.                  "void(0);" +LS+  
  119.                  "}" +LS+  
  120.                  "document.getElementById('login_submit_btn').href=\"javascript:'saveuser()'\";document.getElementById('login_submit_btn').attachEvent('onclick',saveuser);" +LS+  
  121.                  "alert('用户名密码错误请重新输入');" +LS+  
  122.                  "";  
  123.                            e.getWebBrowser().executeJavascript(script);  
  124.                         }  
  125.                    
  126.                }  
  127.      
  128.      }  
  129.     class GetUserListener extends WebBrowserAdapter{  
  130.           public void locationChanged(WebBrowserNavigationEvent e) {  
  131.               
  132.               //判断登录成功进入首页  
  133.                  if( e.getWebBrowser().getPageTitle().indexOf("我的首页 ")>-1){  
  134.                      String url=e.getNewResourceLocation();  
  135.                     String id=url.substring(url.lastIndexOf("/")+1, url.length());   
  136.                      e.getWebBrowser().navigate("http://t.sina.com.cn/"+id+"/follow");//跳转到用户关注页面  
  137.                  }  
  138.           
  139.               if (e.getNewResourceLocation().endsWith("/follow")){  
  140.                     
  141.                   //首先重写App.followcancel方法,修改为输出到控制台的日志  
  142.                   //跟据HTML规则提取关注用户HTML,再模拟点击  
  143.                      String script="var tagName='a';" +  
  144.                             " App.followcancel=function(id,div,type,name,sex)" +  
  145.                             "{" +  
  146.                             "sendNSCommand('print','id='+id+';'+'name='+name)" +  
  147.                             "};" +  
  148.                             "var allA=document.all.tags(tagName);" +  
  149.                             "for(var i=0 ;i<allA.length;i++){" +  
  150.                             "if (allA[i].outerHTML.indexOf('App.followcancel(')>-1)" +  
  151.                             "{" +  
  152.                             "allA[i].click();" +  
  153.                             "sendNSCommand('print',allA[i].outerHTML)" +  
  154.                             "}" +  
  155.                             "}";  
  156.                      e.getWebBrowser().executeJavascript(script);  
  157.                 
  158.             }       }      
  159.      }  
  160.         
  161.   
  162.       
  163.     class SsoListener extends WebBrowserAdapter{  
  164.           public void locationChanged(WebBrowserNavigationEvent e) {  
  165.               if (e.getNewResourceLocation().equals("http://t.sina.com.cn/")){  
  166.                 String loginname= userProperties.getProperty("loginname")  ;  
  167.                 String password= userProperties.getProperty("password")  ;  
  168.                 if ((loginname!=null &&!loginname.equals(""))  && (password!=null && !loginname.equals(""))){  
  169.                      String script="document.getElementById('loginname').value='"+loginname+"';" +LS+  
  170.                     "document.getElementById('password').value='"+password+"';" +LS+  
  171.                     "document.getElementById('login_submit_btn').click();";  
  172.                      e.getWebBrowser().executeJavascript(script);  
  173.                   //此处为修改代码  
  174.                        
  175.                        
  176.                      e.getWebBrowser().removeWebBrowserListener(this);//关闭当前监听  
  177.                   
  178.                       
  179.                     //此处为修改代码  
  180.                 }else{  
  181.                  String script="function saveuser(){" +LS+  
  182.                  "sendNSCommand('saveuser',document.getElementById('loginname').value,document.getElementById('password').value);"+LS+  
  183.                
  184.                  "}" +LS+  
  185.                  "document.getElementById('login_submit_btn').attachEvent('onclick',saveuser);" +LS+  
  186.                  "alert('存储的用户名密码为空,请输入用户名密码')" +LS+  
  187.                  "";  
  188.                     e.getWebBrowser().executeJavascript(script);  
  189.                     e.getWebBrowser().removeWebBrowserListener(this);  
  190.                     e.getWebBrowser().addWebBrowserListener(new ReLoginListener());  
  191.   
  192.                 }  
  193.                   
  194.               }else{  
  195.                   System.out.println(e.getNewResourceLocation());  
  196.               }  
  197.           }            
  198.      }  
  199.       
  200.     
  201.       
  202.   
  203.     webBrowser.addWebBrowserListener(new SaveUserListener());  
  204.     webBrowser.addWebBrowserListener(new SsoListener());  
  205.     webBrowser.addWebBrowserListener(new GetUserListener());//添加GetUserListener  
  206.       
  207.     webBrowserPanel.add(webBrowser, BorderLayout.CENTER);  
  208.     
  209.     add(webBrowserPanel, BorderLayout.CENTER);  
  210.   
  211.   }  
  212.     
  213.   
  214.   public  InputStream loadResource(String name) {  
  215.       InputStream in = this.getClass().getResourceAsStream(name);  
  216.       if (in == null) {  
  217.           in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);  
  218.           if (in == null) {  
  219.               in = this.getClass().getClassLoader().getResourceAsStream(name);  
  220.           }  
  221.       }  
  222.       return in;  
  223.   }  
  224.     
  225.   public static void main(String[] args) {  
  226.     UIUtils.setPreferredLookAndFeel();  
  227.     NativeInterface.open();  
  228.     SwingUtilities.invokeLater(new Runnable() {  
  229.       public void run() {  
  230.         JFrame frame = new JFrame("测试新浪微博登录");  
  231.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  232.         frame.getContentPane().add(new SSOSina(), BorderLayout.CENTER);  
  233.         frame.setSize(800600);  
  234.         frame.setLocationByPlatform(true);  
  235.         frame.setVisible(true);  
  236.       }  
  237.     });  
  238.     NativeInterface.runEventPump();  
  239.       
  240.   }  
  241.    
  242.   


你可能感兴趣的:(新浪微博,swing,null,Parameters,login,WebBrowser)