Java實現:服務端登陸系統並跳轉到系統內的指定頁面(不調用瀏覽器)

Java實現:服務端登陸系統並跳轉到系統內的指定頁面(不調用瀏覽器)

1,思路:根據爬蟲思想;html

2,代碼:web

/** 
 * 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):json

#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
來源:博客園
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。瀏覽器

相關文章
相關標籤/搜索