0

我需要一个读取以下返回 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 读入字符串变量。

4

1 回答 1

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());
    }
}
于 2013-11-07T09:39:47.067 回答