How to get youtube video id from URL with java?

前端 未结 6 2024
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 06:51

I want to get the v=id from youtube\'s URL with java

Example Youtube URL formats:

http://www.youtube.com/watch?v=u8nQa1cJyX8&

6条回答
  •  渐次进展
    2020-12-16 07:51

    I found solution for this .. i expand that URL.. and its working ..

    public static String expandUrl(String shortenedUrl)  {
            URL url;
            String expandedURL = "";
            try {
                url = new URL(shortenedUrl);
                // open connection
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); 
                // stop following browser redirect
                httpURLConnection.setInstanceFollowRedirects(false);
                // extract location header containing the actual destination URL
                expandedURL = httpURLConnection.getHeaderField("Location");
                httpURLConnection.disconnect();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
             return expandedURL;
        }
    

提交回复
热议问题