Just curious to know if we can download youtube videos using downloader using HTTpClient?

import java.io.*;  
import java.net.*;
public class trial {
    public static void main(String[] args){
        setAndAuthenticateProxy();          
            try {
                    URL url = new URL("http://www.youtube.com/watch?v=tQC7rO8cjLs");

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                    connection.setRequestMethod("GET");

                    InputStream in = connection.getInputStream();

                    FileOutputStream out = new FileOutputStream("C://Temp/fo.flv");

                    copy(in, out, 2048);
                    out.close();



                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {

                }



    }


    public static void setAndAuthenticateProxy(){
        System.setProperty("http.proxyHost", "abcxyz");
        System.setProperty("http.proxyPort", "80");
         Authenticator authenticator = new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return (new PasswordAuthentication("username",//username
                            "password".toCharArray()));//password
                }
            };
            Authenticator.setDefault(authenticator);
    }   

    public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
        byte[] buf = new byte[bufferSize];
        int n = input.read(buf);
        while (n >= 0) {
          output.write(buf, 0, n);
          n = input.read(buf);
        }output.flush();
    }
}

Edited 3 Years Ago by abra_ka_dabra

This article has been dead for over six months. Start a new discussion instead.