0

I am using jsoup to make an http post request to login into this page: https://home-access.cfisd.net/HomeAccess/Account/LogOn?ReturnUrl=%2fhomeaccess%2f

    try {
       Response r = Jsoup.connect("https://home-access.cfisd.net/HomeAccess/Account/LogOn?ReturnUrl=%2fhomeaccess%2f")
                .data("LogOnDetails.UserName", "s650665")
                .data("LogOnDetails.Password", "kai66wen").data("Database", "10")
                .method(Method.POST).timeout(10*1000).ignoreHttpErrors(true).execute();
    cookies = r.cookies();
     response.getWriter().println(cookies);
     response.getWriter().println();
   } catch(IOException e){
       System.out.println(e);
       System.exit(0);
   }
    Connection connection = Jsoup.connect(url);

       for(Entry<String, String>cookie : cookies.entrySet()){
               connection.cookie(cookie.getKey(), cookie.getValue());
       }


       response.getWriter().println(connection.ignoreHttpErrors(false).followRedirects(false).userAgent("Mozilla").referrer("http://www.cfisd.net").get());

The above code works just fine when compiling and running in a regular java project within eclipse. But as soon as I transfer this code into the Dynamic Web Project I have setup (With Tomcat server), it does not carry out the http post request properly and does not log in. I am placing the above code in the doGet method of my servlet file in the Dynamic Web Project. Any advice or help is very much appreciated.

user2489946
  • 281
  • 3
  • 12
  • Only a guess.. maybe is Tomcat configured to use url rewriting instead of cookies? Could you try to log in manually (with a browser) and see if something like `jsessionid` appears on the url after the login? Then you'd need to add that `jsessionid` in your second request (or enable cookies in the server). – fonkap Dec 25 '14 at 11:58

2 Answers2

0

I have also encountered this problem, the main reason is that I didn't put the jsoup jar in WebInf/lib, and build classpath directly.

blackbishop
  • 30,945
  • 11
  • 55
  • 76
0

It is JVM encoding difference which makes JSoup post inside container fail. You should set your Tomcat server encoding to UTF-8, which Eclipse set by default for you when you run JSoup directly inside Eclipse. Check this SO post on how to set.

Community
  • 1
  • 1
YourBestBet
  • 1,651
  • 1
  • 12
  • 17