gpt4 book ai didi

java - java中无法下载特定的URL

转载 作者:行者123 更新时间:2023-12-01 17:00:02 25 4
gpt4 key购买 nike

我正在编写以下程序来使用 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。

最佳答案

这个网站似乎受到某些 Web 网关的保护(像 Akamai 这样的 DOS 保护服务?)。客户端似乎通过 TLS 连接和 HTTP 请求( header )进行指纹识别,并且只有有效的 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 中无效(因为协议(protocol)和密码的 TLS 连接不同)。我使用 Apache commons http 客户端尝试了一些组合。但也失败了(即使 Fiddler 也可以执行相同的请求)。

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

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

关于java - java中无法下载特定的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61522906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com