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

 

posted @   整合侠  阅读(4645)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示