如何检索https连接上的cookie? [英] How to retrieve cookies on a https connection?

查看:162
本文介绍了如何检索https连接上的cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将Cookie储存在使用SSL的网址中,但总是传回NULL。

I'm trying to save the cookies in a URL that uses SSL but always return NULL.

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)
任何想法?

I try to capture the cookies the first connection, but always they are always set to NULL. I think it might be some Cois because of the secure connection (HTTPS) Any idea?

推荐答案

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

The issue is not HTTPS. The problem is more or less a small mistake.

要解决您的问题,您只需将 .response()替换为 .execute )。像这样,

To fix your issue you can simply replace .response() with .execute(). Like so,

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 object 之后请求已被执行。显然,如果您没有执行请求, Connection.Response 对象将不会非常有用。


Overall you have to make sure you execute the request first.
Calling .response() is useful to grab the Connection.Response object after the request has already been executed. Obviously the Connection.Response object won't be very useful if you haven't executed the request.

事实上,如果你尝试在未执行的响应上调用 res.body()异常指示问题。

In fact if you were to try calling res.body() on the unexecuted response you would receive the following exception indicating the issue.


java.lang.IllegalArgumentException:必须执行请求(使用.execute(),.get()或.post获取响应正文

java.lang.IllegalArgumentException: Request must be executed (with .execute(), .get(), or .post() before getting response body

这篇关于如何检索https连接上的cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆