【问题标题】:Is it possible to "send" a cookie via Jsoup to a server?是否可以通过 Jsoup 将 cookie “发送”到服务器?
【发布时间】:2016-03-06 23:47:30
【问题描述】:

我正在尝试从网站检索文章价格。问题是,如果您选择在线价格或商店价格,价格会有所不同。选择商店后,网站会创建一个名为:CP_GEODATA 且具有特定值的 cookie。我尝试以不同的方式发送 cookie,但我不断收到在线价格。

public class Parser {

    public static void main(String[] args) throws Exception {
        Map<String, String> cookies =  new HashMap<String, String>();
        cookies.put("CP_COUNTRY ", "%7B%22country%22%3A%22DE%22%7D  ");
        cookies.put("CP_GEODATA ", "%7B%22location%22%3A-1%2C%22firstlocation%22%3A11%2C%22name%22%3A%22Hamburg%22%7D");
        String url = "https://www.cyberport.de/?token=7a2d9b195e32082fec015dca45ba3aa4&sSearchId=565eee12d987b&EVENT=itemsearch&view=liste&query=&filterkategorie=";
        Connection.Response res = Jsoup.connect(url).cookies(cookies).data("query", "4B05-525").execute();
        Document doc = res.parse();
        String tester = doc.select("span[id=articlePrice] > span[class=basis fl]").text();
        String tester2 = doc.select("span[id=articlePrice] > span[class=decimal fl]").text();
        System.out.println(tester + tester2 + " €");
    }
}

我现在取回的价值是 2.90 欧元,但应该是 4.90 欧元。我已经尝试了所有方法并在互联网上进行了很多搜索,但没有找到任何适合我的解决方案。

这是我收到价格的文章: https://www.cyberport.de/micro-usb-2-0-kabel-usb-a-stecker-micro-b-stecker-0-5m--4B05-525_9374.html

我正在尝试接收德国汉堡商店的价格。

您可以在顶部看到我设置的 cookie。

感谢您的帮助!

【问题讨论】:

  • this similar question 中的一些答案似乎很有帮助。
  • @HovercraftFullOfEels 感谢您的评论,但您的链接提供的答案对我没有帮助。
  • 我认为此信息在服务器上,它基于另一个 cookie 中的当前会话。
  • @DavidePastore 找不到任何可以提供帮助的会话 cookie。我想我永远做不到:(

标签: java cookies jsoup


【解决方案1】:

似乎区域信息存储在会话中,并且当您选择区域代码时,它会在帖子中发送到服务器。

那么你需要做以下步骤:

  • 使用所需区域执行 POST
  • 获取会话 cookie
  • 使用这些厨师做您的原始 POST
  • 希望得到正确的结果

这里是代码

public static void main(String[] args) throws Exception {
    Connection.Response res;

    //11 is for Hamburg
    String zoneId = "11";

    //Set the zone and get the session cookies
    res = Jsoup.connect("https://www.cyberport.de/newajaxpass/catalog/itemlist/0/costinfo/" + zoneId)
            .ignoreContentType(true)
            .method(Method.POST).execute();

    final Map<String, String> cookies = res.cookies();
    //print the cookies, we'll see session cookies here
    System.out.println(cookies);

    //If we use that cookies, your code runs Ok
    String url = "https://www.cyberport.de/?token=7a2d9b195e32082fec015dca45ba3aa4&sSearchId=565eee12d987b&EVENT=itemsearch&view=liste&query=&filterkategorie=";
    res = Jsoup.connect(url).cookies(cookies).data("query", "4B05-525").execute();

    Document doc = res.parse();
    String tester = doc.select("span[id=articlePrice] > span[class=basis fl]").text();
    String tester2 = doc.select("span[id=articlePrice] > span[class=decimal fl]").text();
    System.out.println(tester + tester2 + " €");

    //Extra check
    System.out.println(doc.select("div.townName").text());
}

你会看到:

{SERVERID=realmN03, SCS=76fe7473007c80ea2cfa059f180c603d, SID=pphdh7otcefvc5apdh2r9g0go2}
4,90 €
Hamburg

我希望这是理想的结果。

【讨论】:

    猜你喜欢
    • 2016-07-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2021-05-14
    • 2011-06-08
    • 2021-12-10
    相关资源
    最近更新 更多