4

Iam trying to display webpage on JEditorPane, but getting error at

JEditorPane editor = new JEditorPane(url);

Below is code which i workout.

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);


    }

}

This is the error iam getting

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

Please let me know how can i resolve this issue.

developer
  • 9,116
  • 29
  • 91
  • 150

3 Answers3

4

Although I don't know what are you using the proxy for, as a piece of answer I would include its reference directly in the connection opening. i.e. instead of declaring it as system properties to be sure that you are effectively using it. It would give something like that:

SocketAddress proxySocketAdress= new InetSocketAddress("Proxy IP address", 8080);
Proxy proxy=new Proxy(Proxy.Type.HTTP,proxySocketAdress);
HttpURLConnection  uc = (HttpURLConnection) url.openConnection(proxy);

I hope this helps,

B.F.

bear foot
  • 105
  • 5
3

I was able to trick Google into thinking I was a different browser by changing the http.agent property. The 403 was immediately resolved.

You can do so by running the following line before the rest of your code:

System.setProperty("http.agent", "Mozilla/5.0");

I imagine there are other things you could set http.agent to that would work, but this worked for me so I left it alone. I formulated it from the answer to this question: Setting user agent of a java URLConnection

Community
  • 1
  • 1
Bit Fracture
  • 651
  • 1
  • 9
  • 24
1

See HTTP 403.

In the HTTP used on the World Wide Web, 403 Forbidden is an HTTP status code returned by a web server when a user requests a web page or media that the server does not allow them to. In other words, the server can be reached, but the server declined to allow access to the page.

(Moments later..)

URL url=new URL("http://www.google.com");

Gee, what a surprise. [ OK, that was sarcasm. ;) ]

Google is notorious for being the 'example URL' that people cannot connect to. It is largely because they do not offer up the labors of their efforts for use by 'any old application'. There was a (very restricted) Google API for about 5 minutes, but it was withdrawn long ago.

I see the code does some 'fibbing' about what it is. That is apparently not enough to fool Google. (And to be plain, I'm not about to expend effort trying to figure how to get around those protections - if Google does not want their pages served in your app, that is their business.)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433