使用SAXBuilder和代理URL

时间:2013-06-27 20:12:37

标签: java xml xml-parsing proxy

如何处理解析存储在代理服务器后面的XML文件?我有以下代码:

import java.io.IOException;
import java.net.URL;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class FileParsing {
    public static void parseXMLFile(String xmlFilePath) throws IOException, JDOMException {
            URL xmlFileURL = new URL(xmlFilePath);
            SAXBuilder builder = new SAXBuilder();
            Document xmlDoc = builder.build(xmlFileURL);

            // File parsing code...
    }
}

现在我得到了这个例外:

 java.io.IOException: Server returned HTTP response code: 502 for URL: https://proxyserver.com/xmlFileOfInterest.xml

我假设这是因为该文件位于代理服务器上。这是我的问题的原因吗?如果是这样,处理代理服务器上的文件的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用apache webclient来解析它

WebClient client = WebClient.create(xmlFilePath
                ,String.class,null);    
        client = client.type(MediaType.APPLICATION_XML);

HTTPConduit httpConduit = (HTTPConduit)WebClient.getConfig(client).getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();               
        policy.setProxyServer(proxyurl);
        policy.setProxyServerPort(8080);
        httpConduit.setClient(policy);

    Response r = client.post(); 
InputStream response= (InputStream)r.getEntity();