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