我需要一个读取以下返回 XML 的 URL 的 java 代码。
我尝试了下面的代码,但给出了java.net.ConnectException。但是如果我直接在浏览器中点击 URL,我可以在浏览器页面中获取 xml。谁能帮我我哪里出错了?
编码::
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String urllink="http://dev.virtualearth.net/REST/v1/Locations?o=xml&culture=en-US&postalCode=2811&key=AvTXuyNkBKfm4wFGPxSDfA6jvN0dNlq6OhAg8wuw4zFLokJFgv8ivclIkq1nJTIo";
String strProxy = "http://proxy.ebiz.verizon.com";
// URI requestURI = new URI("http", "proxy.ebiz.verizon.com", "dev.virtualearth.net", 80,"/REST/v1/Locations/" + "culture=en-US&postalCode=2811", "key=AvTXuyNkBKfm4wFGPxSDfA6jvN0dNlq6OhAg8wuw4zFLokJFgv8ivclIkq1nJTIo",null);
URL url = new URL("http","proxy.ebiz.verizon.com", 80,urllink);
// String jsonResponse = getResponse(url);
Properties sysProps = System.getProperties();
sysProps.put("proxySet", "true");
sysProps.put("proxyHost", "172.31.1.3");
sysProps.put("proxyPort", "8080");
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("userID","password".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
long lTime = System.currentTimeMillis();
StringBuffer sb = new StringBuffer("");
String http = url.toString();
// Create a connection.
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
// urlConnection.setRequestProperty("USER-AGENT", "Mozilla/2.02Gold (WinNT; I)");
urlConnection.setRequestProperty("Content-type", "application/json");
urlConnection.setAllowUserInteraction(true);
urlConnection.connect();
InputStream in =
((HttpURLConnection) urlConnection).getInputStream();
int length = urlConnection.getContentLength();
for (int n = 0; n < length; n++) {
sb.append((char) in.read());
}
System.out.println( sb.toString());
}catch(Exception e)
{
System.out.println("Exception:: "+e.getMessage());
}
}
我想将 XML 读入字符串变量。