0

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.

Community
  • 1
  • 1
DoctorDo
  • 331
  • 2
  • 6
  • 15
  • @BalusC, if i'm doing like http://stackoverflow.com/questions/7206133/sending-post-request-and-save-cookies/7206268#7206268 there is the same error. – DoctorDo Dec 03 '15 at 12:41
  • You need to change the data if your html form is different. This is then applicable as duplciate: http://stackoverflow.com/q/12175763 – BalusC Dec 03 '15 at 13:06
  • @BalusC, i have to login programmatically and get sessionId.How can i get content of needed page? please help – DoctorDo Dec 04 '15 at 03:26
  • the task is, i have to stay in the same session and parse needed html page, if it possible, if not possible, i have to login programmatically, keep sessionId and then parse needed html page – DoctorDo Dec 04 '15 at 05:33

1 Answers1

0
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

DoctorDo
  • 331
  • 2
  • 6
  • 15