简体   繁体   English

Http请求进行远程XML解析

[英]Http Request for remote XML parsing

i'm stuck on this process from two days, before posting i've searched a lot of topic and looks like it's a so simple issue. 我从发布开始就停留了两天,在发布之前,我已经搜索了很多主题,看起来这是一个非常简单的问题。 But i didn't get the problem. 但是我没有问题。

Scenario is basic: i want to parse an XML from a remote computer through http connection: 场景是基本的:我想通过http连接从远程计算机解析XML:

  import java.io.*;
  import java.net.HttpURLConnection;
  import java.net.URL;
  try {
       URL url = new URL("http://host:port/file.xml");
       HttpURLConnection connection = (HttpURLConnection) url.openConnection();
       connection.setRequestMethod("GET");
       connection.setRequestProperty("Accept","application/xml");
       InputStream is = connection.getInputStream();
       BufferedReader br = new BufferedReader(new InputStreamReader(is));
       PrintWriter pw = new PrintWriter("localfile_pw.xml");
       FileOutputStream fos = new FileOutputStream("localfile_os.xml");

Then i tried three different ways to read the XML 然后我尝试了三种不同的方式来读取XML

Reading byte stream 读取字节流

   byte[] buffer = new byte[4 * 1024];
   int byteRead;
   while((byteRead= is.read(buffer)) != -1){
                fos.write(buffer, 0, byteRead);
    }

Reading charachter per character 每个角色阅读字符

   char c;
   while((c = (char)br.read()) != -1){
          pw.print(c);
          System.out.print(c);
    }

Reading line per line 每行读取行

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

In all cases my xml reading stops at the same point, after the same exact nuumber of bytes . 在所有情况下,我的xml读取都在相同的精确字节数之后停止在同一点。 And gets stuck without reading and without giving any exception. 并被卡住而没有阅读,也没有给出任何例外。

Thanks in advance. 提前致谢。

How about this (see IOUtils from Apache): 怎么样(请参阅Apache的IOUtils ):

URL url = new URL("http://host:port/file.xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept","application/xml");
InputStream is = connection.getInputStream();
FileOutputStream fos = new FileOutputStream("localfile_os.xml");
IOUtils.copy(is, fos);
is.close();
fos.close();

The class supports persistent HTTP connections by default. 该类默认情况下支持持久HTTP连接。 If the size of the response is know at the time of the response, after it sends your data, the server will wait for another request. 如果在响应时知道响应的大小,则在发送数据后,服务器将等待另一个请求。 There are 2 ways of handling this: 有两种处理方法:

  1. Read the content-length: 阅读内容长度:

     InputStream is = connection.getInputStream(); String contLen = connection.getHeaderField("Content-length"); int numBytes = Integer.parse(contLen); 

    Read numBytes bytes from the input stream. 从输入流中读取numBytes个字节。 Note: contLen may be null, in this case you should read until EOF. 注意: contLen可以为null,在这种情况下,您应该阅读直到EOF。

  2. Disable connection keep alive: 禁用连接保持活动状态:

     connection.setRequestProperty("Connection","close"); InputStream is = connection.getInputStream(); 

    After sending the last byte of data the server will close the connection. 发送完最后一个数据字节后,服务器将关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM