2015-12-03 42 views
0

帮助我与jsoup登录,我必须登录,然后从登录页面重定向到另一个页面与保存的会话。java.io.IOException:403错误加载URL jsoup登录

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(); 

它抛出下面的异常

产生java.io.IOException:403错误加载URL http://localhost:8080/dir/dir/index.xhtml

如果我做像[Sending POST request with username and password and save session cookie它抛出同样的异常。

+0

@BalusC,如果我做喜欢http://stackoverflow.com/questions/7206133/sending-post -request-and-save-cookies/7206268#7206268存在相同的错误。 – DoctorDo

+0

如果您的html表单不同,您需要更改数据。这适用于duplciate:http://stackoverflow.com/q/12175763 – BalusC

+0

@BalusC,我必须以编程方式登录并获取sessionId.How我可以得到所需页面的内容?请帮助 – DoctorDo

回答

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()); 

最后我得到了它,我不知道这是否是正确的,但工程