HttpUrlConnection using proxy

I use HttpURLconnection using proxy in the following way:

addr =new InetSocketAddress(ip,port);

proxy =new Proxy(Proxy.Type.HTTP, addr);

conn = (HttpURLConnection)url.openConnection(proxy);

and after a lot of such connections I try to use the HttpURLconnection without proxy in the following way:

conn = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);

The problem is that it does use proxy ene though.

[519 byte] By [rutia] at [2007-10-3 11:06:57]
# 1

HttpURLConnection may connect to a url through a proxy when specifically requested to use no proxy. This is indeed a bug.

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

I logged Bug No. 6498566 to track this issue. It may take a few days to become available in the public database. But when it does it will accessible through the following url:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6498566

One workaround is to disable the persistent connection/keep alive cache by setting -Dhttp.keepAlive=false

see http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html

chegara at 2007-7-15 13:29:50 > top of Java-index,Archived Forums,Socket Programming...
# 2

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.InetSocketAddress;

import java.net.Proxy;

import java.net.ProxySelector;

import java.net.URI;

import java.net.URL;

import java.util.Iterator;

import java.util.List;

import java.util.Properties;

/*

* TestURLCon.java

*

* Created on December 12, 2006, 11:02 AM

*/

/**

*

* @author Jagadish

*/

public class TestURLCon {

/** Creates a new instance of TestURLCon */

public TestURLCon() {

}

public static void main(String[] args) {

try {

System.setProperty("java.net.useSystemProxies","true");

String hostname=new String();

String port=new String();

String url="http://www.yahoo.com/";

List l = ProxySelector.getDefault().select(new URI(url));

for (Iterator iter = l.iterator(); iter.hasNext(); ) {

Proxy proxy = (Proxy) iter.next();

System.out.println("proxy hostname : " + proxy.type());

InetSocketAddress addr = (InetSocketAddress)proxy.address();

if(addr == null) {

System.out.println("No Proxy");

} else {

System.out.println("proxy hostname : " + addr.getHostName());

hostname=addr.getHostName();

System.out.println("proxy port : " + addr.getPort());

port=String.valueOf(addr.getPort());

}

URL server = new URL(url);

Properties systemProperties = System.getProperties();

systemProperties.setProperty("http.proxyHost",hostname);

systemProperties.setProperty("http.proxyPort",port);

HttpURLConnection connection = (

HttpURLConnection)server.openConnection();

connection.connect();

InputStream in = connection.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(in));

StringBuilder sb = new StringBuilder();

String line = null;

while ((line = br.readLine()) != null) {

sb.append(line + "\n");

}

br.close();

System.out.println(sb.toString());

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

zagsa at 2007-7-15 13:29:50 > top of Java-index,Archived Forums,Socket Programming...