1

I wrote the following code to automate download video from youtube...

but I stuck in the middle and get ERROR 403 from the server!

What I plan to do is to post the link with some parameters to the server and then extract the download link from the response and then download the file direcly....

what is wrong? can anybody please help me?

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();
    }
}
}
zigzag
  • 97
  • 1
  • 8

0 Answers0