JDK-6498566 : URL.openConnection(Proxy.NO_PROXY) may connect through a proxy.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0u11,6,6u5
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2006-11-29
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b05Fixed
Related Reports
Duplicate :  
Duplicate :  
Description
HttpURLConnection may connect to a url through a proxy when specifically requested to use no proxy. 

see http://forum.java.sun.com/thread.jspa?threadID=790417&tstart=0

The may happen as follows:
1) request resource from foo.bar through a proxy
2) read response and close the InputStream
3) HttpURLConnecion implementation will cache the connection
4) request resource from foo.bar with no proxy
5) HttpURLConnecion implementation may reuse the cached connection from 3.

The sample code below shows the problem. The second request will be made through the proxy sever.

--- begin code ---
import java.net.*;
import java.io.*;

public class TestHttpCache
{
    private static String proxyStr = "proxyServer";   // replace with own proxy server
    private static int proxyPort = 8080;              // replace with own proxy port
    private static String urlStr = "http://foo.bar/"; // replace with own http server
    
    private static InetSocketAddress addr = new InetSocketAddress(proxyStr, proxyPort);
    private static Proxy proxyServer = new Proxy(Proxy.Type.HTTP, addr);
    
    public static void main(String[] args) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxyServer);
        InputStream is = uc.getInputStream();
        
        byte[] ba = new byte[1024];
        while(is.read(ba) != -1);
        is.close();
        
        // 2nd connection.
        uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        is = uc.getInputStream();
        
        while(is.read(ba) != -1);
        is.close();
        
    }
}
---end code---

Comments
SUGGESTED FIX src/share/classes/sun/net/www/http/HttpClient.java ------- if (ret != null) { if ((ret.proxy != null && ret.proxy.equals(p)) || (ret.proxy == null && p == null)) { synchronized (ret) { ret.cachedHttpClient = true; assert ret.inCache; ret.inCache = false; } + } else { + // We cannot return this connection to the cache as it's + // KeepAliveTimeout will get reset. We simply close the connection. + // This should be fine as it is very rare that a connection + // to the same host will not use the same proxy. + ret.inCache = false; + ret.closeServer(); + ret = null; } } } if (ret == null) { ret = new HttpClient(url, p, to); } else { SecurityManager security = System.getSecurityManager(); if (security != null) {
18-10-2010

EVALUATION The problem is that we can connect to a URL through a proxy even if URL.openConnection(Proxy.NO_PROXY) is specified. This only happens if there is already a cached connection to the same protocol://host:port. The fix is to verify that the same proxy is being used by cached connections.
08-12-2006