hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Randall Scarberry <drran...@yahoo.com>
Subject Using HttpClient with a proxy from an untrusted applet
Date Thu, 21 Aug 2008 21:43:56 GMT

I've been going in circles all day trying to use HttpClient in an untrusted applet to download
data from the server through a proxy.  Finally, I found a way to obtain the proxy information
in the applet, but the information cannot be put to use without generating an AccessControlException.

The old version of our code that uses a simple URLConnection works transparently.  It just
inherits the settings from the browser.  HttpClient apparently needs the info to be explicitly
set, but applet security constraints get in the way.  Is HttpClient not meant to be used in
applets?  Or only with trusted applets?   

Here's an example of what I'm trying to do:

        String urlSpec = "http://servername/mediawiki/index.php?title=somewikipage";
        HttpClient client = new HttpClient();
        try {
            // Get the proxy information.  This works.
            // System.getProperty() calls generate AccessControlExceptions.
            ProxyInfo info[] = ProxyService.getProxyInfo(new URL(urlSpec));
            if(info != null && info.length > 0 && info[0] != null) {
                String proxyHost = info[0].getHost();
                int proxyPort = info[0].getPort();
                if (proxyHost != null && proxyHost.length() > 0 && proxyPort
> 0) {
                    // Set the proxy info in the host configuration.                
                    client.getHostConfiguration().setProxy(proxyHost, proxyPort);
        }catch (Exception ex) {
              "could not retrieve proxy configuration, attempting direct connection.");

        GetMethod method = new GetMethod(urlSpec + "&action=edit");
        int responseCode = 200;
        String responseBody = null;
        // Manually set the cookies.
        // Call method which returns the cookies as one string.
        String cookies = getCookieString();
        if (cookies.length() > 0) {
            method.setRequestHeader("Cookie", cookies);
        try {
            responseCode = client.executeMethod(method);
            // ... Do stuff with the response.
        } finally {
Here are the relevant parts of the stack trace.  Note that the AccessControlException occurs
when the get method is executed, not when the proxy information is set.

Caused by: java.security.AccessControlException: access denied (java.net.SocketPermission connect,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 java.net.Socket.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.<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 < -- from: "responseCode = client.executeMethod(method);" -->
Any help would be greatly, greatly appreciated!

Randy S


To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org

View raw message