【问题标题】:Reading a URL which returns XML--JAVA读取返回 XML--JAVA 的 URL
【发布时间】:2013-11-07 08:59:55
【问题描述】:

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

【问题讨论】:

    标签: java xml url httpurlconnection


    【解决方案1】:

    您可能会收到 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());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2019-06-21
      • 2014-07-02
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多