Author Options:

Can you fix my Java code? Answered

I have this.

import javax.swing.JLabel;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.JFrame;
public class WebViewer
  public static void main(String[] argv) throws Exception
    URL url = new URL("http://www.youtube.com");

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null)
      JLabel textLabel = new JLabel(str);

I'm getting an error that says

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.youtube.com
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
    at java.net.URL.openStream(URL.java:1010)
    at WebViewer.main(WebViewer.java:15)

The code should be getting a url's content and printing it to the screen. I'm using youtube as a test. Can you help me?