Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server returned HTTP response code: 403 for URL: Iam displaying webpage on JEditorPane

Tags:

java

swing

awt

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.

like image 696
developer Avatar asked Feb 20 '23 19:02

developer


2 Answers

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.

like image 64
bear foot Avatar answered Feb 23 '23 07:02

bear foot


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

like image 27
Bit Fracture Avatar answered Feb 23 '23 09:02

Bit Fracture