JEditorPane で Web ページを表示しようとしていますが、次の場所でエラーが発生します
JEditorPane editor = new JEditorPane(url);
以下は私がワークアウトするコードです。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import com.sun.org.apache.xml.internal.security.utils.Base64;
public class webpageDisplay {
/**
* @param args
* @throws IOException
*/
static class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
// I haven't checked getRequestingScheme() here, since for NTLM
// and Negotiate, the usrname and password are all the same.
System.err.println("Feeding username and password for " + getRequestingScheme());
return (new PasswordAuthentication("UserId","Password".toCharArray()));
}
}
public static void main(String[] args) throws IOException {
System.getProperties().put( "proxySet", "true" );
System.setProperty("http.proxyHost", "I given proxy host");
System.setProperty("http.proxyPort", "8080");
Authenticator.setDefault(new MyAuthenticator());
URL url=new URL("http://www.google.com");
HttpURLConnection uc = (HttpURLConnection) url.openConnection ();
uc.addRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
uc.connect();
JEditorPane editor = new JEditorPane(url);
editor.setEditable(false);
JScrollPane pane = new JScrollPane(editor);
JFrame f = new JFrame("HTML Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(pane);
f.setSize(800, 600);
f.setVisible(true);
}
}
これは私が得ているエラーです
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.com
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:45)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
at java.lang.reflect.Constructor.newInstance(Constructor.java:515)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1291)
at java.security.AccessController.doPrivileged(AccessController.java:251)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1285)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:939)
at javax.swing.JEditorPane.getStream(JEditorPane.java:823)
at javax.swing.JEditorPane.setPage(JEditorPane.java:429)
at javax.swing.JEditorPane.<init>(JEditorPane.java:256)
at webpageDisplay.main(webpageDisplay.java:48)
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1236)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:384)
at javax.swing.JEditorPane.getStream(JEditorPane.java:788)
... 3 more
この問題を解決する方法を教えてください。