如何在java中使用代理获取URL连接?

我试图在运行时使用代理创建URL连接。 我的代码如下:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80)); HttpURLConnection connection = (HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy); 

但这不起作用。 谁知道为什么?

添加答案以帮助未来的访客

 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80)); HttpURLConnection connection =(HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "text/xml"); connection.setRequestProperty("Accept", "text/xml, application/xml"); connection.setRequestMethod("POST"); 

dku.rajkumar的方式不适用于我。

我试试这个,它的工作原理。 但它需要两倍的时间。

 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80)); HttpURLConnection connection = (HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy); ((HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy)).getInputStream(); System.out.println(connection.usingProxy()); 

结果是真的

没有((HttpURLConnection)new URL("http://abc.example.com").openConnection(proxy)).getInputStream();

结果是错误的