我编写了以下代码来自动从youtube下载视频......
但我卡在中间并从服务器获得ERROR 403!
我打算做的是将带有一些参数的链接发布到服务器,然后从响应中提取下载链接,然后直接下载文件....
有什么不对?有人可以帮帮我吗?
import java.net.*;
import java.io.*;
import java.util.*;
import java.nio.charset.*;
public class Downloader{
public static void main(String[] args){
try{
String downloadLink = "https://www.youtube.com/watch?v=cRvl8zqAt_E&index=121&list=PL9394655A98259F67";
URL url = new URL("http://convert2mp3.net/en/index.php");
String addr = "127.0.0.1";
int port = 1180;
Map<String,Object> params = new LinkedHashMap<>();
params.put("url", downloadLink);
params.put("format", "mp4");
params.put("quality", "720");
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0)
postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
// I have to use proxy i.e. my ISP blocks youtube!
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(addr , port));
HttpURLConnection connection = (HttpURLConnection)url.openConnection(proxy);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
connection.setDoOutput(true);
connection.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
}catch(Exception ex){
ex.printStackTrace();
}
}
}