2012-09-21 66 views
3

我试图将cookie保存在使用SSL但始终返回NULL的URL中。如何检索https连接上的cookie?

private Map<String, String> cookies = new HashMap<String, String>(); 

    private Document get(String url) throws IOException { 
      Connection connection = Jsoup.connect(url); 
      for (Entry<String, String> cookie : cookies.entrySet()) { 
       connection.cookie(cookie.getKey(), cookie.getValue()); 
      } 
      Response response = connection.execute(); 
      cookies.putAll(response.cookies()); 
      return response.parse(); 
     } 

    private void buscaJuizado(List<Movimentacao> movimentacoes) { 
      try { 
       Connection.Response res = Jsoup       .connect("https://projudi.tjpi.jus.br/projudi/publico/buscas/ProcessosParte?publico=true") 
    .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2") 
    .timeout(0) 
    .response(); 
    cookies = res.cookies(); 
    Document doc = get("https://projudi.tjpi.jus.br/projudi/listagens/DadosProcesso? numeroProcesso=" + campo); 
    System.out.println(doc.body()); 
    } catch (IOException ex) { 
    Logger.getLogger(ConsultaProcessoTJPi.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

我尝试捕获cookie的第一个连接,但总是将它们设置为NULL。我认为这可能是一些Cois,因为安全的连接(HTTPS) 任何想法?

回答

1

该问题不是HTTPS。这个问题或多或少是一个小错误。

要解决您的问题,您可以简单地将.response()替换为.execute()。像这样,

private void buscaJuizado(List<Movimentacao> movimentacoes) { 
    try { 
    Connection.Response res = Jsoup 
     .connect("https://projudi.tjpi.jus.br/projudi/publico/buscas/ProcessosParte?publico=true") 
     .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2") 
     .timeout(0) 
     .execute(); // changed fron response() 
    cookies = res.cookies(); 
    Document doc = get("https://projudi.tjpi.jus.br/projudi/listagens/DadosProcesso?numeroProcesso="+campo); 
    System.out.println(doc.body()); 
    } catch (IOException ex) { 
    Logger.getLogger(ConsultaProcessoTJPi.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 


总的来说,你必须确保你第一次执行请求。
调用.response()是有用抓住Connection.Response对象后的请求已经被执行。如果你没有执行请求,显然Connection.Response对象不会很有用。

事实上,如果你要尝试,你会收到以下异常指示问题未执行响应调用res.body()

java.lang.IllegalArgumentException异常:请求必须得到响应体

+0

没办法之前执行(与.execute(),获得(),或。员额()...我做到了但不成功 – user1689105