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
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

转载于:https://www.cnblogs.com/lizm166/p/8205085.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值