FULL PRODUCT VERSION :
java full version "1.6.0_05-b13"
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
HttpURLConnection connect to a old url (without proxy) when specifically requested to use proxy.
Reproduce as follows:
1) request resource from an url
2) read response and close the InputStream
3) HttpURLConnecion implementation will cache the connection
4) request resource from the same url through proxy
5) HttpURLConnecion implementation may reuse the cached connection from 3.
The sample code below shows the problem.
import java.net.*;
import java.io.*;
public class HttpCacheTest {
private static String proxyStr = "localhost"; // replace with own proxy server
private static int proxyPort = 8888; // replace with own proxy port
private static String urlStr = "http://localhost:7001/NoWebWS/NoWebWSService?WSDL"; // 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);
byte[] ba = new byte[1];
HttpURLConnection uc = null;
InputStream is = null;
uc = (HttpURLConnection) url.openConnection();
is = uc.getInputStream();
while (is.read(ba) != -1)
System.out.print(new String(ba));
is.close();
System.out.println("\n\n------------------------------------------------------");
System.out.println("------------------------------------------------------\n");
// 2nd connection.
uc = (HttpURLConnection) url.openConnection(proxyServer);
is = uc.getInputStream();
while (is.read(ba) != -1)
System.out.print(new String(ba));
is.close();
}
}
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.net.*;
import java.io.*;
public class HttpCacheTest {
private static String proxyStr = "localhost"; // replace with own proxy server
private static int proxyPort = 8888; // replace with own proxy port
private static String urlStr = "http://localhost:7001/NoWebWS/NoWebWSService?WSDL"; // 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);
byte[] ba = new byte[1];
HttpURLConnection uc = null;
InputStream is = null;
uc = (HttpURLConnection) url.openConnection();
is = uc.getInputStream();
while (is.read(ba) != -1)
System.out.print(new String(ba));
is.close();
System.out.println("\n\n------------------------------------------------------");
System.out.println("------------------------------------------------------\n");
// 2nd connection.
uc = (HttpURLConnection) url.openConnection(proxyServer);
is = uc.getInputStream();
while (is.read(ba) != -1)
System.out.print(new String(ba));
is.close();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
set system property -Dhttp.keepAlive=false
But it will disable the connection pool advantage. performance issue.
A complete solution is to update the KeepAliveKey in sun.net.www.http.KeepAliveCache. To append proxy as a key. This means:
(url, object, noproxy) != (url,object,proxy)