1

In the past, a similar code like the following worked well to get just one or two exchange rates from the bank's XML data - without applying the overhead of an XML parser.

But now the characters read seem to have nothing to do with that URL, although when entering the web address in my browser, the page still looks fine.

String adr = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";

try {
    URL url = new URL(adr);
    InputStream in = url.openStream();
    byte[] b = new byte[1024];
    int l = 0;

    while ((l = in.read(b)) != -1) {
        String s = new String(b, 0, l);
        System.out.println(l);
        System.out.println(s);
    }

    in.close();
} catch (MalformedURLException e) {
    System.out.println(e);
} catch (IOException e) {
    System.out.println(e);
}

What happened and what do I have to do to get the xml data?

MTCoster
  • 5,868
  • 3
  • 28
  • 49
Jörg
  • 214
  • 1
  • 9
  • What you’re parsing there is the body of the 307 Internal Redirect returned for that URL - your code should follow this chain until a 200 response is achieved. See also [this answer](https://stackoverflow.com/a/26046079/1813169) – MTCoster Feb 02 '19 at 23:01
  • Confirm with one comment - request to HTTP returns 301 Moved Permanently – Bor Laze Feb 02 '19 at 23:10

1 Answers1

2

Page moved - use https url ("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml")

BTW, why do you read from URL in such complex way? Use RestTemplate:

    RestTemplate restTemplate = new RestTemplate();
    String url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
    ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

    System.out.println(response.getBody());
Bor Laze
  • 2,458
  • 12
  • 20
  • Oooh, how I love these one character errors! Thank you very much, Bor. – Jörg Feb 02 '19 at 23:08
  • Ref. RestTemplate. - The given URL is the only one I read in the web so far, and thus I never worked with the spring framework. But I will note your hint for possible future use. Thanks again. – Jörg Feb 02 '19 at 23:50