【问题标题】:Not able to download specific URL in java无法在java中下载特定的URL
【发布时间】:2020-04-30 12:01:24
【问题描述】:

我正在编写以下程序以使用 Apache Common-IO 下载 URL,但我收到了 ReadTimeOut 异常, 例外

java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.security.ssl.InputRecord.readFully(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
at sun.security.ssl.AppInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1456)
at com.touseef.stock.FileDownload.main(FileDownload.java:23)

程序

String urlStr = "https://www.nseindia.com/";
    File file = new File("C:\\User\\WorkSpace\\Output.txt");
    URL url;
    try {
        url = new URL(urlStr);
        FileUtils.copyURLToFile(url, file);
        System.out.println("Successfully Completed.");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

其他网站可以下载。请建议。 使用 commons-io-2.6 jar。

【问题讨论】:

  • 使用不同的方法copyURLToFile(URL source, File destination, int connectionTimeout,int readTimeout) 并指定更长的超时时间。由于 readTimeout 太小而导致错误发生。
  • 感谢您的评论。使用不同的方法和更长的超时,仍然是同样的问题。仅限特定网站。其他站点很容易到达。上述网站可在网络浏览器中无缝打开。
  • 这个问题是特定于这个服务器的。我假设它需要某些 HTTP 标头,否则它不会返回任何结果。因此你根本不能使用FileUtils.copyURLToFile。您必须手动打开 HTTP 连接,设置请求标头并继续。

标签: java url download apache-commons


【解决方案1】:

该站点似乎受到某些 Web 网关(如 Akamai 之类的 DOS 保护服务?)的保护。客户端似乎通过 TLS 连接和 HTTP 请求(标头)进行指纹识别,并且只有有效的 Web 浏览器才能连接到该站点。

以下代码使用Apache commons http client 4.5 并且至少目前有效:

    String urlStr = "https://www.nseindia.com/";
    File file = new File("C:\\User\\WorkSpace\\Output.txt");
    String userAgent = "-";

    CloseableHttpClient httpclient = HttpClients.custom().setUserAgent(userAgent).build();
    HttpGet httpget = new HttpGet(urlStr);
    httpget.addHeader("Accept-Language", "en-US");
    httpget.addHeader("Cookie", "");

    System.out.println("Executing request " + httpget.getRequestLine());
    try (CloseableHttpResponse response = httpclient.execute(httpget)) {
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        String body = EntityUtils.toString(response.getEntity());
        System.out.println(body);
        Files.writeString(file.toPath(), body);
    }

在 Firefox 中有效的请求在 Java 中无效(因为与协议和密码的 TLS 连接不同)。我使用 Apache commons http 客户端尝试了一些组合。但也失败了(即使 Fiddler 发出了相同的请求)。

因此,在 Java 中使用这个网站非常困难,即使上面的代码目前也可以工作,保护系统可以随时调整,使其无法再次工作。

我假设这样的网站提供了专用于程序使用的 API。联系他们并询问,这是我能给你的唯一建议。

【讨论】:

  • 感谢您的回答。通过使用 ui4j 和 jsoup api 修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 2012-10-19
相关资源
最近更新 更多