1

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 Answer 1

    -1

    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.

    8
    • 1
      This doesn't address the OP's issue.
      – Ben
      CommentedJun 17, 2014 at 7:51
    • Thank you very much, but i still get the same error. On your code it crashes on: InputStream in = url.openConnection().getInputStream();CommentedJun 17, 2014 at 7:53
    • I edited my first answer, works good at my place. Maybe you will give it a try.
      – ÂlexBay
      CommentedJun 17, 2014 at 8:39
    • Thank you very much again. Does it work every time? It works sometimes, but usally the first time i get a java.net.SocketExeption (The error i posted). Fails at InputStreamReader(url.openStream());CommentedJun 17, 2014 at 8:46
    • I tried it several times and did not get any Exceptions for now. First I thought it was a Problem with https, but it seems it runs with https also. EDIT: Finally i got the Problem using "https" instead of "http"... maybe this is your problem?
      – ÂlexBay
      CommentedJun 17, 2014 at 8:49

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.