【问题标题】:Server returned HTTP response code: 403 for URL: Iam displaying webpage on JEditorPane服务器返回 HTTP 响应代码:403 用于 URL:我正在 JEditorPane 上显示网页
【发布时间】:2015-08-11 13:07:06
【问题描述】:

我试图在 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

请告诉我如何解决这个问题。

【问题讨论】:

  • 您必须将代理添加到连接中,而不是添加到系统属性中

标签: java swing awt


【解决方案1】:

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

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

希望对你有帮助,

B.F.

【讨论】:

    【解决方案2】:

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

    您可以通过在其余代码之前运行以下行来做到这一点:

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

    我想你可以设置其他的东西 http.agent 来工作,但这对我有用,所以我不管它。我根据这个问题的答案制定了它: Setting user agent of a java URLConnection

    【讨论】:

      【解决方案3】:

      HTTP 403

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

      (稍后……)

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

      哎呀,真是个惊喜。 [好吧,那是讽刺。 ;) ]

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

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

      【讨论】:

      • 哦,汤普森,你又出现了。我现在有信心了。但它正在连接,我检查了“uc.connect();”只有当 url 传递给 JEditorPane 时它​​才会抛出异常
      • @Thompson 有什么惊喜,请告诉我
      • 抱歉,我无法联系到您。 JEdi​​torPan 没有获取 url?
      • +1 另见rfc2606,例如example.com.
      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      相关资源
      最近更新 更多