1
votes

I have a simple code that fetches a XML file from the web. More specific, a XML file with exchange rates from a norwegian bank. The problem is, it only works sometimes. Usually, it never works the first time.

After some testing, I'm pretty sure its crashes on InputStreamReader(url.openStream()). But as a said, not every time.

So, is it any better way to to this, and ensure that it fetches the XML file correctly

All help is appriciated.

public class xmlReader {

private static Properties intProps;

public static void main(String[] args) {
    intProps = new Properties();

    intProps.setProperty("SOURCEURL", "https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml");
    intProps.setProperty("SOURCEDIR", "C:\\Users\\");
    intProps.setProperty("SOURCEFILE", "EXCHANGERATES_#YYYYMMDD#_#U#.xml");     
    fetchURL();
}


private static void fetchURL() {
    try {

        URL url = new URL(intProps.getProperty("SOURCEURL"));
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));


        FileOutputStream fout = new FileOutputStream(intProps.getProperty("SOURCEDIR") + intProps.getProperty("SOURCEFILE"));

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
                   System.out.println("Line: " + inputLine);
          fout.write(inputLine.getBytes());                        
        }

        fout.close();

        in.close();

    } catch(Exception e) {
                  e.printStackTrace();
                 // writeLog("Error fetching from URL: " + e.getMessage());                                                                           
    }    

}

The error is as following:

java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at sun.security.ssl.InputRecord.readFully(Unknown Source) at sun.security.ssl.InputRecord.read(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.waitForClose(Unknown Source) at sun.security.ssl.HandshakeOutStream.flush(Unknown Source) at sun.security.ssl.Handshaker.kickstart(Unknown Source) at sun.security.ssl.SSLSocketImpl.kickstartHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

1

1 Answers

-1
votes

Everytime I want do download a file, I use this code ( or derviations )

try {   
 URL url;
 url = new URL("http://YourWebSourceXML.xml");  


 InputStream in = url.openConnection().getInputStream();
 OutputStream out = new FileOutputStream("YourDestFile.xml");
 byte[] buffer = new byte[1024];
 for (int n;(n = in.read(buffer)) != -1;
         out.write(buffer, 0, n));

 in.close();
 out.close(); }
 catch (IOException e){
 // Do something
 }

EDIT:

I got this working on first try:

 try {

    URL url = new URL("http://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));


    FileOutputStream fout = new FileOutputStream("x.xml");

    String inputLine;
    while ((inputLine = in.readLine()) != null) {
               System.out.println("Line: " + inputLine);
      fout.write(inputLine.getBytes());                        
    }

    fout.close();

    in.close();

} catch(Exception e) {
              e.printStackTrace();
             // writeLog("Error fetching from URL: " + e.getMessage());                                                                           
}    

Quick and dirty, maybe this will help.