4

我试图在 JEditorPane 上显示网页,但出现错误

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

请让我知道如何解决此问题。

4

3 回答 3

4

虽然我不知道您使用代理做什么,但作为一个答案,我会将其引用直接包含在连接打开中。即,而不是将其声明为系统属性,以确保您有效地使用它。它会给出类似的东西:

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

我希望这有帮助,

高炉

于 2012-04-17T09:34:20.060 回答
3

http.agent通过更改属性,我能够欺骗 Google 认为我是不同的浏览器。403 立即解决。

您可以通过在其余代码之前运行以下行来执行此操作:

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

我想还有其他可以设置http.agent的东西会起作用,但这对我有用,所以我不理会它。我从这个问题的答案中制定了它: Setting user agent of a java URLConnection

于 2015-08-11T05:07:06.460 回答
1

请参阅HTTP 403

在万维网上使用的 HTTP 中,403 Forbidden是当用户请求服务器不允许他们访问的网页或媒体时,Web 服务器返回的 HTTP 状态代码。换句话说,服务器可以访问,但服务器拒绝允许访问该页面。

(片刻之后..)

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

啧啧,真是惊喜。[好吧,那是讽刺。;) ]

谷歌因成为人们无法连接的“示例 URL”而臭名昭著。这主要是因为他们没有提供他们的努力以供“任何旧应用程序”使用。有一个(非常受限的)Google API 大约 5 分钟,但很久以前就撤回了。

我看到代码对它是什么做了一些“捏造”。这显然不足以愚弄谷歌。(坦率地说,我不打算花精力去想办法绕过这些保护措施——如果谷歌不希望他们的页面在你的应用程序中提供,那是他们的事。)

于 2012-04-17T08:47:22.757 回答