package org.chenxi.learn.connection; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class ProxyTest { public static final String APPLICATION_URI = "http://www.abcd.com/abcd"; public static void main(String[] args) throws IOException, DocumentException { InetSocketAddress addr = new InetSocketAddress("proxy.abcd.com",8080); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); URL url = new URL(APPLICATION_URI); URLConnection conn = url.openConnection(proxy); InputStream in = conn.getInputStream(); // 格式化输出 XML SAXReader reader = new SAXReader(); Document doc = reader.read(in); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(format); writer.write(doc); writer.close(); } }