简体   繁体   中英

java.io.IOException: 403 error loading URL jsoup login

help me with jsoup login, i have to login, then redirect from login page to another page with saved session.

public void parseXhtml() throws IOException{
         String sessionID=null;
         Map<String, String> cookies = new HashMap<String, String>();
        cookies.put("login", "login");
        cookies.put("password", "password");
        Connection conn=Jsoup.connect("http://localhost:8080/dir/login.xhtml");

        Connection.Response res = Jsoup
                .connect("http://localhost:8080/dir/login.xhtml")
                .data(cookies)
                .execute();
        res = Jsoup.connect("http://localhost:8080/dir/dir/index.xhtml")
                .cookie("JSESSIONID", res.cookies().get("JSESSIONID"))
                .method(Method.GET)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36")
                .execute();

         Document doc = res.parse();
         System.out.println(doc.html());
         sessionID = res.cookie("JSESSIONID");

         Document docu = Jsoup.connect("http://localhost:8080/dir/dir/index.xhtml")
                            .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36")
                            .cookie("JSESSIONID", res.cookies().get("JSESSIONID"))
                            .method(Connection.Method.GET)
                            .get();

it throws the below exception

java.io.IOException: 403 error loading URL http://localhost:8080/dir/dir/index.xhtml

if i'm doing like [ Sending POST request with username and password and save session cookie it throws the same exception.

ExternalContext ec=FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest req=(HttpServletRequest) ec.getRequest();
        HttpSession sess=(HttpSession) ec.getSession(true);
        String url = req.getRequestURL().append(";jsessionid=").append(sess.getId()).toString();

        ec.setRequest("http://localhost:8080/dir/dir/index.xhtml");
        HttpServletRequest req2=(HttpServletRequest) ec.getRequest();
        String url2 = req2.getRequestURL().append(";jsessionid=").append(sess.getId()).toString();
        Document doc2=Jsoup.connect(url2).get();
        System.out.println(doc2.html());

Finally i got it, I do not know if it is right, but works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM