使用JAVA从url下载二进制文件,给出403错误,但PHP工作正常

时间:2015-12-13 15:15:14

标签: java php curl httpurlconnection http-status-code-403

我从一个网站上下载了一些用于下载mp3的 java 代码。它提供 403 状态代码。我用谷歌搜索并得知我需要设置 用户代理 推荐人 (因为他们需要它) 。我设置那两个仍然不起作用。所以我捕获了我发送到我所获得的网站的请求,我需要发送的内容和内容。我设置了所有必需的标题,我得到了相同的403.我对此进行了小规模的研究,我使用 php windows curl (来自curl.haxx.se的curl.exe),它运行正常。

这是我写的代码

使用Apache HttpClient的Java(给出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());

使用URL和URLConnection的Java(给出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

使用curl的PHP(获取输出)

<?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响应?

提前致谢。

1 个答案:

答案 0 :(得分:1)

虽然我现在自己得到了404错误,但我非常有信心你有一个多余的R

Referer 参考 r

我的代码,基于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());