1

我创建了一个使用来自 Apache HTTP 组件的 HttpClient 的小程序。该小程序应该打开一个远程网页并将页面的源打印到控制台。该代码在从 IDE (NetBeans) 运行时运行良好,但是在网页中部署时会引发 AccessControlException:

access denied ("java.net.SocketPermission" "www.hardwarebase.net" "resolve")

在阅读有关该问题的类似问题后,似乎问题出在未签名的小程序中。我是自己签名的,所以当我下次运行它时,小程序要求并获得了权限。

不过,我得到了同样的例外。这是主小程序类的代码:

public class MyApplet extends java.applet.Applet {
    public DefaultHttpClient client;
        public String useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1";
    public void init() 
    {                   
        resize(150,25);

        client = new DefaultHttpClient();       
        String page = Get("http://www.hardwarebase.net"));
        System.out.println(page);   

    }

    public String Get(String url)
    {
        final HttpUriRequest request = new HttpGet(url);
        request.setHeader("User-Agent", useragent);
        HttpResponse response;
        try {
            response = (HttpResponse)AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
                            try {
                                return client.execute(request);
                            } catch (IOException ex) {
                                Logger.getLogger(MyApplet.class.getName()).log(Level.SEVERE, null, ex);
                            }

                            return null;
                        }
                    }
                );
                return _getResponseBody(response.getEntity());
        } catch (IOException e) {
                e.printStackTrace();
        }   

        return "";
    }

    public String Post(String url, List<NameValuePair> nameValuePairs)
    {
        final HttpPost request = new HttpPost(url);
        request.setHeader("User-Agent", useragent);
        try {
                    request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response;

                    try {
                        response = (HttpResponse)AccessController.doPrivileged(new PrivilegedAction() {
                                public Object run() {
                                        try {
                                            return client.execute(request);
                                        } catch (IOException ex) {
                                            Logger.getLogger(MyApplet.class.getName()).log(Level.SEVERE, null, ex);
                                        }

                                        return null;
                                    }
                                }
                            );
                        return _getResponseBody(response.getEntity());
                    } catch (IOException e) {
                            e.printStackTrace();
                    }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public void paint(Graphics g) 
    {
        g.drawString("Hello world!", 50, 25);
    }

    public String _getResponseBody(final HttpEntity entity) throws IOException, ParseException 
    {
        if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); }
        InputStream instream = entity.getContent();
        if (instream == null) { return ""; }
        if (entity.getContentLength() > Integer.MAX_VALUE) { throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); }
        String charset = getContentCharSet(entity);
        if (charset == null) { charset = HTTP.DEFAULT_CONTENT_CHARSET; }
        Reader reader = new InputStreamReader(instream, charset);
        StringBuilder buffer = new StringBuilder();
        try 
        {
            char[] tmp = new char[1024];
            int l;
            while ((l = reader.read(tmp)) != -1) 
            {
                buffer.append(tmp, 0, l);
            }   
        } 
        finally 
        {
            reader.close();
        }

        return buffer.toString();

    }

    public String getContentCharSet(final HttpEntity entity) throws ParseException 
    {
        if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); }
        String charset = null;
        if (entity.getContentType() != null) 
        {
            HeaderElement values[] = entity.getContentType().getElements();

            if (values.length > 0) 
            {   
                NameValuePair param = values[0].getParameterByName("charset");

                if (param != null) { charset = param.getValue(); }  
            }
        }

        return charset;
    }

}

如何解决此 AccessControlException?

4

1 回答 1

0

我通过从 HttpClient 切换到 URLConnection 解决了这个问题。我不确定为什么 URLConnection 不会抛出异常,而 HttpClient 会。

于 2012-08-05T12:01:14.070 回答