Question

I have created an applet which uses the HttpClient from Apache HTTP Components. The applet is supposed to open a remote webpage and print the source of the page to console. The code works fine when ran from IDE (NetBeans), however when deployed in a webpage it throws an AccessControlException:

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

Upon reading similar questions concerning the problem, it seemed like the problem was in the applet not being signed. I self-signed it, so when I ran it the next time, the applet asked for and got the permissions.

Still, I get the same exception. Here is the code of the main applet class:

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;
    }

}

How can I resolve this AccessControlException?

Was it helpful?

Solution

I have resolved the issue by switching from HttpClient to URLConnection. I am not sure why doesn't URLConnection throw the exception, while HttpClient does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top