2012-04-17 19 views
2

上のWebページを表示しますが、Serverは、HTTP応答コードを返しました:403 URLのために:IAMがIAMのがJEditorPaneの上でWebページを表示しようとする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 

を取得エラーIAMです。

+0

をあなたはそれではなく、システムのプロパティ – mKorbel

答えて

1

HTTP 403を参照してください。

ワールド・ワイド・ウェブ上で使用されるHTTPでは

、を禁止ユーザーは、サーバーがそれらをすることはできませんので、Webページやメディアを要求すると、Webサーバから返されたHTTPステータスコードです。つまり、サーバーにアクセスできますが、サーバーはページへのアクセスを許可しませんでした。

(しばらくして...)

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

うわあ、どのような驚き。 [OK、それは皮肉でした。 ;)]

Googleは、人々が接続できない「例のURL」であることで有名です。これは主に、「古いアプリケーション」で使用するための努力の労力を提供していないためです。 (非常に制限された)Google APIが約5分間ありましたが、それはずっと前に取り下げられました。

私は、コードがそれが何であるかについていくつかの「フィブリング」をしているのを見ます。それは明らかにGoogleをだますには不十分です。 (そして、私は、Googleが自分のアプリケーションであるページをあなたのビジネスに提供したくないとすれば、それらの保護を回避する方法を見つけようと努力しているわけではない。)

+0

ああトンプソンに、接続にプロキシを追加する必要があり、再びあなたは今、自信を持ってpicture.Iに入って来ました。しかし、それは接続している、私は "uc.connect();をチェックしたURLがJEditorPaneに渡っているときに例外がスローされます – developer

+0

@Thompsonは何を驚かせますか、私に知らせてください – developer

+0

申し訳ありませんが、私はあなたを取得できません。 JEdi​​torPanはURLを受け取りませんか? – developer

4

私は、プロキシを使用していることを知っています。回答の一部として、接続の参照に直接その参照を含めることになります。つまり、システムプロパティとして宣言する代わりに、効果的に使用していることを確認する必要があります。それはそのような何かを与えるだろう:

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

私は私がhttp.agentプロパティを変更することにより、別のブラウザだったという考えにグーグルをだますことができました

B.F.

1

、このことができます願っています。 403は直ちに解決された。

あなたはあなたのコードの残りの部分の前に次の行を実行することによって行うことができます:私はあなたがうまくいくとhttp.agentを設定することができ、他のものがあると想像

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

が、これは私のために働いていたので、私は左それだけで。私はこの質問への回答から、それを定式化: Setting user agent of a java URLConnection

関連する問題