简体   繁体   中英

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:

  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

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 . And gets stuck without reading and without giving any exception.

Thanks in advance.

How about this (see IOUtils from Apache):

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. 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. Note: contLen may be null, in this case you should read until 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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