读取返回XML的URL - JAVA

时间:2013-11-07 08:59:56

标签: java xml url httpurlconnection

我需要一个java代码,它读取以下返回XML的URL。

我尝试了以下代码,但提供了 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读入字符串变量。

1 个答案:

答案 0 :(得分:0)

您可能会收到 java.net.ConnectException ,因为您尝试从代理后面点击URL。我猜你要么是代理的用户名和密码,要么是错误的,或代理不起作用。

以下对我有用:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        // Create URL
        String urllink = "http://dev.virtualearth.net/REST/v1/Locations?o=xml&culture=en-US&postalCode=2811&key=AvTXuyNkBKfm4wFGPxSDfA6jvN0dNlq6OhAg8wuw4zFLokJFgv8ivclIkq1nJTIo";
        URL url = new URL(urllink);
        StringBuffer sb = new StringBuffer();

        // Create a connection.
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setRequestProperty("Content-type", "application/json");
        urlConnection.setAllowUserInteraction(true);

        urlConnection.connect();

        InputStream stream = urlConnection.getInputStream();
        InputStreamReader isReader = new InputStreamReader(stream);
        BufferedReader br = new BufferedReader(isReader);

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            sb.append(line);
        }

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

        stream.close();
        br.close();
        urlConnection.disconnect();


    } catch (Exception e) {
        System.out.println("Exception:: " + e.getMessage());
    }
}