Applet + Http获取其他Url的请求

时间:2011-06-03 17:54:54

标签: java applet httpclient

我有以下课程:

public class TestApplet extends JApplet {

    private static final long serialVersionUID = -2137477433249866949L;

    private JTextArea display;

    @Override
    public void init() {
        //Create the text area and make it uneditable.
        display = new JTextArea( 1, 80 );
        display.setEditable( false );

        //Set the layout manager so that the text area
        //will be as wide as possible.
        setLayout( new GridLayout( 1, 0 ) );

        //Add the text area to the applet.
        add( new JScrollPane( display ) );

        String getResult = getResult();

        display.setText( getResult );

    }

    public static void main( final String[] args ) {
        System.out.println( new TestApplet().getResult() );
    }

    private String getResult() {
        String getResult = "";
        try {
            GetMethod getMethod = new GetMethod( "http://www.google.com" );
            HttpClient httpClient = new HttpClient();

            httpClient.executeMethod( getMethod );

            getResult = getMethod.getResponseBodyAsString();
        }
        catch ( Exception exception ) {
            getResult = ExceptionUtils.getFullStackTrace( exception );
        }
        return getResult;
    }

当我在本地运行它时工作正常。但是,当我在应用服务器上运行它时,我得到:

java.security.AccessControlException: access denied (java.net.SocketPermission www.google.com resolve)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkConnect(Unknown Source)
    at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at java.net.InetSocketAddress.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
    at com.test.TestApplet.init(TestApplet.java:40)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

jar 签名,所以我不确定我在这里做错了什么。有没有人看到任何明显的问题?

感谢。

编辑#1: 这是html源代码:

<html>
<title>
1.1 TestApplet
</title>
<body>
<h1>
TestApplet
</h1>

    <applet 
        code="com.test.TestApplet" 
        archive="testapplet-1.0.0-SNAPSHOT-jar-with-dependencies.jar"
        width=100% 
        height=100%>
    </applet>

</body>
</html>

编辑#2

运行请求命令的输出:

C:\ workspaces \ workspace-helios-main \ testapplet \ target&gt; jarsigner -verify testapplet-1.0.0-SNAPSHOT.jar jar验证。

警告: 此jar包含签名者证书已过期的条目。

使用-verbose和-certs选项重新运行以获取更多详细信息。

1 个答案:

答案 0 :(得分:1)

将小程序的init()换入AccessController#doPrivileged()

public void init() {
    AccessController.doPrivileged(new PrivilegedAction<Object> {
        @Override 
        public Object run() {
            // Put your original init() here.
            return null;
        }
    });
}

另见: