使用 JAVA 从 url 下载二进制文件,出现 403 错误,但 PHP 工作正常
Download binary file from url using JAVA giving 403 error but PHP working fine
我写了一些 java 代码用于从一个网站下载 mp3。它给出 403 状态码。我用谷歌搜索了一下,发现我需要设置 User-Agent 和 Referrer(因为他们需要)。我设置了这两个仍然没有用。所以我捕获了我发送到我得到的网站的请求,我需要发送的内容和所有内容。我设置了所有必需的 header 并且得到了相同的 403。我对此做了一些小研究,我使用了 php 和 windows curl(curl.exe 来自 curl.haxx.se)并且工作正常。
这是我写的代码
Java 使用 Apache HttpClient(给出 403)
HttpClient httpClient = new HttpClient();
GetMethod get = new GetMethod("http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
get.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
get.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
get.setRequestHeader("Referrer", "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
httpClient.executeMethod(get);
System.out.println(get.getStatusCode());
Java 使用 URL 和 URL 连接(给出 403)
URL u = new URL("http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
HttpURLConnection uc = (HttpURLConnection) u.openConnection(); // Without type cast to HttpURLConnection , URLConnection also not working
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
uc.addRequestProperty("Referrer", "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
uc.connect();
System.out.println(uc.getResponseMessage());// Forbidden output
PHP 使用 curl(获取输出)
<?php
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
curl_setopt ($curl, CURLOPT_REFERER, "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
$contents = curl_exec($curl);
echo $contents;
exit;
?>
Windows curl(获取输出)
curl "http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36" -H "Referer: http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html" --compressed
我在这里做错了什么?为什么我在使用 Java 时无法获得 200 响应?
提前致谢。
尽管我自己现在遇到 404 错误,但我非常有信心您有一个多余的 R
。
是Referer不是Referrer
我的代码,基于 HttpClient 4.5:
HttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
get.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
get.setHeader(HttpHeaders.REFERER, "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
HttpResponse res = httpClient.execute(get);
System.out.println(res.getStatusLine());
我写了一些 java 代码用于从一个网站下载 mp3。它给出 403 状态码。我用谷歌搜索了一下,发现我需要设置 User-Agent 和 Referrer(因为他们需要)。我设置了这两个仍然没有用。所以我捕获了我发送到我得到的网站的请求,我需要发送的内容和所有内容。我设置了所有必需的 header 并且得到了相同的 403。我对此做了一些小研究,我使用了 php 和 windows curl(curl.exe 来自 curl.haxx.se)并且工作正常。
这是我写的代码
Java 使用 Apache HttpClient(给出 403)
HttpClient httpClient = new HttpClient();
GetMethod get = new GetMethod("http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
get.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
get.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
get.setRequestHeader("Referrer", "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
httpClient.executeMethod(get);
System.out.println(get.getStatusCode());
Java 使用 URL 和 URL 连接(给出 403)
URL u = new URL("http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
HttpURLConnection uc = (HttpURLConnection) u.openConnection(); // Without type cast to HttpURLConnection , URLConnection also not working
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
uc.addRequestProperty("Referrer", "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
uc.connect();
System.out.println(uc.getResponseMessage());// Forbidden output
PHP 使用 curl(获取输出)
<?php
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
curl_setopt ($curl, CURLOPT_REFERER, "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
$contents = curl_exec($curl);
echo $contents;
exit;
?>
Windows curl(获取输出)
curl "http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36" -H "Referer: http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html" --compressed
我在这里做错了什么?为什么我在使用 Java 时无法获得 200 响应?
提前致谢。
尽管我自己现在遇到 404 错误,但我非常有信心您有一个多余的 R
。
是Referer不是Referrer
我的代码,基于 HttpClient 4.5:
HttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://redu.su/download_file.php?id=7745333&filename=k-j-yesudas-mavapanisa-silpa.mp3&ts=130944042611814004&hash=b825427b3a63b28c8a4ba3b2a34294fb");
get.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
get.setHeader(HttpHeaders.REFERER, "http://redmp3.cc/7745324/k-j-yesudas-sreeragamo.html");
HttpResponse res = httpClient.execute(get);
System.out.println(res.getStatusLine());