为什么服务器 returns 向我发送 java 中有效文件的响应代码 403?
Why server returns me the Response Code 403 for a valid file in java?
我想通过java得到这个文件的Content Length
:
https://www.subf2m.co/subtitles/farsi_persian-text/SImp4fRrRnBK6j-u2RiPdXSsHSuGVCDLz4XZQLh05FnYmw92n7DZP6KqbHhwp6gfvrxazMManmskHql6va6XEfasUDxGevFRmkWJLjCzsCK50w1lwNajPoMGPTy9ebCC0&name=Q2FwdGFpbiBNYXJ2ZWwgRmFyc2lQZXJzaWFuIGhlYXJpbmcgaW1wYWlyZWQgc3VidGl0bGUgLSBTdWJmMm0gW3N1YmYybS5jb10uemlw
当我在 Firefox
或 Google Chrome
中插入此 url 时,它会下载一个文件。但是当我想通过 Java HttpsURlConnection
、服务器 returns 我 Response Code 403
和 Content Length -1
查看该文件的大小时。为什么会这样?谢谢
try {
System.out.println("program started -----------------------------------------");
String str_url = "https://www.subf2m.co/subtitles/farsi_persian-text/SImp4fRrRnBK6j-u2RiPdXSsHSuGVCDLz4XZQLh05FnYmw92n7DZP6KqbHhwp6gfvrxazMManmskHql6va6XEfasUDxGevFRmkWJLjCzsCK50w1lwNajPoMGPTy9ebCC0&name=Q2FwdGFpbiBNYXJ2ZWwgRmFyc2lQZXJzaWFuIGhlYXJpbmcgaW1wYWlyZWQgc3VidGl0bGUgLSBTdWJmMm0gW3N1YmYybS5jb10uemlw";
URL url = new URL(str_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setConnectTimeout(150000);
con.setReadTimeout(150000);
con.setRequestMethod("HEAD");
con.setInstanceFollowRedirects(false);
con.setRequestProperty("Accept-Encoding", "identity");
con.setRequestProperty("connection", "close");
con.connect();
System.out.println("responseCode: " + con.getResponseCode());
System.out.println("contentLength: " + con.getContentLength());
} catch (IOException e) {
System.out.println("error | " + e.toString());
e.printStackTrace();
}
输出:
program started -----------------------------------------
responseCode: 403
contentLength: -1
默认值 Java user-agent 被某些在线服务(最著名的是 Cloudflare)阻止。您需要将 User-Agent
header 设置为其他值。
con.setRequestProperty("User-Agent", "My-User-Agent");
根据我的经验,设置什么并不重要,只要它不是默认值即可:
con.setRequestProperty("User-Agent", "aaa"); // works perfectly fine
编辑: 看起来这个站点使用 Cloudflare,DDoS 保护处于活动状态 - 您的代码不会 运行 Java 实际获取文件内容所需的脚本挑战.
我想通过java得到这个文件的Content Length
:
https://www.subf2m.co/subtitles/farsi_persian-text/SImp4fRrRnBK6j-u2RiPdXSsHSuGVCDLz4XZQLh05FnYmw92n7DZP6KqbHhwp6gfvrxazMManmskHql6va6XEfasUDxGevFRmkWJLjCzsCK50w1lwNajPoMGPTy9ebCC0&name=Q2FwdGFpbiBNYXJ2ZWwgRmFyc2lQZXJzaWFuIGhlYXJpbmcgaW1wYWlyZWQgc3VidGl0bGUgLSBTdWJmMm0gW3N1YmYybS5jb10uemlw
当我在 Firefox
或 Google Chrome
中插入此 url 时,它会下载一个文件。但是当我想通过 Java HttpsURlConnection
、服务器 returns 我 Response Code 403
和 Content Length -1
查看该文件的大小时。为什么会这样?谢谢
try {
System.out.println("program started -----------------------------------------");
String str_url = "https://www.subf2m.co/subtitles/farsi_persian-text/SImp4fRrRnBK6j-u2RiPdXSsHSuGVCDLz4XZQLh05FnYmw92n7DZP6KqbHhwp6gfvrxazMManmskHql6va6XEfasUDxGevFRmkWJLjCzsCK50w1lwNajPoMGPTy9ebCC0&name=Q2FwdGFpbiBNYXJ2ZWwgRmFyc2lQZXJzaWFuIGhlYXJpbmcgaW1wYWlyZWQgc3VidGl0bGUgLSBTdWJmMm0gW3N1YmYybS5jb10uemlw";
URL url = new URL(str_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setConnectTimeout(150000);
con.setReadTimeout(150000);
con.setRequestMethod("HEAD");
con.setInstanceFollowRedirects(false);
con.setRequestProperty("Accept-Encoding", "identity");
con.setRequestProperty("connection", "close");
con.connect();
System.out.println("responseCode: " + con.getResponseCode());
System.out.println("contentLength: " + con.getContentLength());
} catch (IOException e) {
System.out.println("error | " + e.toString());
e.printStackTrace();
}
输出:
program started -----------------------------------------
responseCode: 403
contentLength: -1
默认值 Java user-agent 被某些在线服务(最著名的是 Cloudflare)阻止。您需要将 User-Agent
header 设置为其他值。
con.setRequestProperty("User-Agent", "My-User-Agent");
根据我的经验,设置什么并不重要,只要它不是默认值即可:
con.setRequestProperty("User-Agent", "aaa"); // works perfectly fine
编辑: 看起来这个站点使用 Cloudflare,DDoS 保护处于活动状态 - 您的代码不会 运行 Java 实际获取文件内容所需的脚本挑战.