Jsoup Post请求不在Tomcat上使用Dynamic Web Project

时间:2014-12-24 20:08:31

标签: java eclipse http tomcat7 jsoup

我正在使用jsoup发出http post请求以登录此页面: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());

上面的代码在eclipse中的常规java项目中编译和运行时效果很好。但是,只要我将此代码传输到我已设置的动态Web项目(使用Tomcat服务器),它就不会正确执行http post请求而不会登录。我将上面的代码放在我的doGet方法中动态Web项目中的servlet文件。非常感谢任何建议或帮助。

2 个答案:

答案 0 :(得分:0)

我也遇到过这个问题,主要原因是我没有将jsoup jar放在WebInf/lib中,而是直接构建类路径。

答案 1 :(得分:0)

JVM编码差异使得容器内的JSoup帖子失败。您应该将Tomcat服务器编码设置为UTF-8,当您在Eclipse中直接运行JSoup时,Eclipse默认为您设置。请查看SO post如何设置。