【问题标题】:Downloading a file with HTTP GET, passing cookies in java使用 HTTP GET 下载文件,在 java 中传递 cookie
【发布时间】:2016-07-19 02:27:56
【问题描述】:

我想从 Java 中的 URL 解析 HTML 文档。

当我在浏览器 (chrome) 中输入 url 时,它不会显示 html 页面,但会下载它。

因此,网址是网页上“下载”按钮后面的链接。 到目前为止没有问题。网址是“https://www.shazam.com/myshazam/download-history”,如果我将其粘贴到浏览器中,它可以正常下载。但是当我尝试用java下载它时,我得到一个401(禁止)错误。

我在加载 url 时检查了 chrome 网络工具,发现我的配置文件数据和注册 cookie 是通过 http GET 传递的。

我尝试了很多不同的方法,但都没有奏效。所以我的问题是,我如何用 java 生成它?如何获取(下载)HTML 文件并对其进行解析?

更新:

这是我们目前发现的(感谢 Andrew Regan):

BasicCookieStore store = new BasicCookieStore();
store.addCookie( new BasicClientCookie("profile-data", "value") );  // profile-data
store.addCookie( new BasicClientCookie("registration", "value") );  // registration
Executor executor = Executor.newInstance();
String output = executor.use(store)
            .execute(Request.Get("https://www.shazam.com/myshazam/download-history"))
            .returnContent().asString();

最后一行代码似乎导致了 NullPointerException。其余代码似乎可以正常加载不受保护的网页。

【问题讨论】:

    标签: java authentication cookies shazam


    【解决方案1】:

    因此,如果您删除这些 cookie/使用私人会话,浏览器应该会重现您在代码中看到的内容。

    我猜你需要先去“http://www.shazam.com/myshazam”并登录。

    【讨论】:

    • shazam的问题是,你只需要输入你的电子邮件地址登录,然后在浏览器中打开mai。不是标准的用户名/密码有点交易。
    【解决方案2】:

    您可以尝试将 cookie 值添加到 GET 请求中,例如使用 HttpClient Fluent API:

    CookieStore store = new BasicCookieStore();
    store.addCookie( new BasicClientCookie(name, value) );  // profile-data
    store.addCookie( new BasicClientCookie(name, value) );  // registration
    
    Executor executor = Executor.newInstance();
    String output = executor.cookieStore(store)
            .execute(Request.Get("https://www.shazam.com/myshazam/download-history"))
            .returnContent().asString();
    

    要解析,你可以这样做:

    Element dom = Jsoup.parse(output);
    for (Element element : result.select("tr td")) {
        String eachCellValue = element.text();
        // Whatever
    }
    

    (您没有提供更多细节)

    【讨论】:

      【解决方案3】:

      我自己找到了答案。使用 HttpURLConnection,此方法可用于对各种服务进行“身份验证”。我使用 chrome 的内置网络工具来获取 GET 请求的 cookie 值。

      HttpURLConnection con = (HttpURLConnection) new URL("https://www.shazam.com/myshazam/download-history").openConnection();
      con.setRequestMethod("GET");
      con.addRequestProperty("Cookie","registration=Cooki_Value_Here;profile-data=Cookie_Value_Here");
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String inputLine;
          while ((inputLine = in.readLine()) != null) 
          System.out.println(inputLine);
          in.close();
      

      【讨论】:

        猜你喜欢
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 2012-02-29
        • 2011-03-16
        • 1970-01-01
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        相关资源
        最近更新 更多