美文网首页
java模拟登陆

java模拟登陆

作者: Fa1se003 | 来源:发表于2017-09-01 18:23 被阅读246次
    import java.io.IOException;
    import java.util.HashMap;
    import org.jsoup.Connection;
    import org.jsoup.Connection.Method;
    import org.jsoup.Connection.Response;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    
    public class PullHtml {
    
        public static void main(String[] args) {
            // 登陆
            String url = "http://127.0.0.1/session/index.php/welcome/";
            // 登陆后访问的页面
            String url2 = "http://127.0.0.1/session/index.php/welcome/test";
    
            try {
    
                Connection conn = Jsoup.connect(url);
                conn.method(Method.POST);
                conn.followRedirects(false);
                // 填写用户名密码
                conn.data("name", "admin").data("pwd", "admin");
                Response response = conn.execute();
    
                // 获取登陆后的cookie
                HashMap<String, String> cookies = (HashMap<String, String>) response.cookies();
    
                //设置cookie访问
                Document doc = Jsoup.connect(url2).cookies(cookies).get();
    
                System.out.println(doc.body().text());
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    }
    

    相关文章

      网友评论

          本文标题:java模拟登陆

          本文链接:https://www.haomeiwen.com/subject/tqpkjxtx.html