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

 

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Python3.x:pip命令安装第三方库,超时处理方案详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。