zoukankan      html  css  js  c++  java
  • Java实现:服务端登录系统并跳转到系统内的指定页面(不调用浏览器)

    Java实现:服务端登录系统并跳转到系统内的指定页面(不调用浏览器)

    1,思路:根据爬虫思想;

    2,代码:

    /** 
     * ClassName:AuthFr 
     * Function: TODO
     * Reason:   TODO 
     * Date:     2018年01月05日 上午14:02:44 
     * @author   lizm  
     * @since    JDK 1.6 
     *       
     */
    public class Test {
        
        public static Map<String, String> headers = null;
        static {
            headers = new HashMap<String, String>();
            headers.put("User-Agent",Pub.getPropertiesValue("datasource", "User-Agent"));
            headers.put("Accept-Language", Pub.getPropertiesValue("datasource", "Accept-Language"));
            headers.put("Accept-Encoding",  Pub.getPropertiesValue("datasource", "Accept-Encoding"));
            headers.put("Accept", Pub.getPropertiesValue("datasource", "Accept"));
            headers.put("Connection", Pub.getPropertiesValue("datasource", "Connection"));
            headers.put("Content-Type", Pub.getPropertiesValue("datasource", "Content-Type"));
        }
    
        //Java实现:服务端登录系统并跳转到系统内的指定页面(不打开浏览器)
        public void gotoUrl(){
            //登录页面url+用户名,密码等参数
            String url = "";
            url = Pub.getPropertiesValue("datasource", "finebi.login.url");
            //系统内指定的页面(可带参数)
            String url2 = "";
            url2 = Pub.getPropertiesValue("datasource", "finebi.cancle.url");
            //登录页面
            org.jsoup.Connection conn = JsoupHelper.getConnection(url, null, "utf-8", headers);
            try {
                Response response = conn.execute();
                String result = response.body();
                //System.out.println("返回的json字符串login:"+result );
                Robot r = new  Robot();
                //延时执行
                r.delay(10000);  
                //重定向页面(必须是同一个conn,不然会跳到登录页面)
                conn.url(url2);
                response = conn.execute();
                result = response.body();
                //System.out.println("返回的json字符串cancle:" +result);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) throws IOException {
            Test client = new  Test();
            client.gotoUrl();
        }

    3,对应的属性文件(datasource.properties):

    #web url
    finebi.login.url=登录url
    #web url
    finebi.cancle.url=访问页面url
    
    #head 
    User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36
    Accept-Language=zh-CN,zh;q=0.8
    Accept-Encoding=gzip, deflate, sdch, br
    Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Connection=Keep-Alive
    Content-Type=application/json;charset=UTF-8

    作者:整合侠
    链接:http://www.cnblogs.com/lizm166/p/8205085.html
    来源:博客园
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    C# WebApi 获取客户端ip地址
    C# 构造函数快捷键
    2 .SHELL 5~10节
    Spring项目集成apidoc生成api接口文档
    根据域名查找对应的ip及端口
    高质量SQL的30条建议
    1 .shell编程1~5
    CentOS7安装mysql8
    编译安Apache2.4.43报错checking for APR... no configure: error: APR not found. Please read the documentation.
    rm -rf * 的正确用法
  • 原文地址:https://www.cnblogs.com/lizm166/p/8205085.html
Copyright © 2011-2022 走看看