最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Reading ECB currency exchange rates from the web - unable to find valid certification - Stack Overflow

programmeradmin5浏览0评论

In the past I could read the current exchange rates provided by the European Central Bank as xml data with the following code. Also when entering the web address given in the code in a browser, the xml structure was nicely displayed.

But the behaviour changed recently:

  • The code now throws a
    javax.ssl.SSLHandshakeException:
    PKIX path building failed:
    Sun.security.provider.certpath.SunCertPathBuilderException:
    unable to find valid certification path to requested target
  • When entering the web address in a browser, the xml file is no more displayed, but immediately and automatically downloaded in the default download folder.

    So I am looking for a way either
  • to provide the requested certification to java or
  • to programatically initiate the download, for then I could read the data from the downloaded file.

    There are several links in the web treating the certification, but to my understanding they are mostly tied to a specific framework or library. The solution most voted for on Stackoverflow is here, but I fail already at step 1, as Firefox doesn't show the lock icon, when entering an address ending with xml.


    P.S.: I barely need to access web pages in my programming tasks; that's why I never had to care for certificates so far. If this becomes a must now to understand a suggested solution, any tutorial or other learning link is welcome.
import java.io.*;
import java.*;

public class ReadURLTest {
    static String webAddress;

    public static void main(String[] args) {
        if (args.length==0)
            webAddress= ".xml";
        else
            webAddress= args[0];
        try {
            URL url = new URI(webAddress).toURL();
            InputStream in = url.openStream();
            byte[] b = new byte[700];
            int l= in.readNBytes(b, 0, b.length);
            System.out.println(l);
            in.close();
            String s= new String(b, 0, l);
            System.out.println(s);
        } catch (MalformedURLException ex) {
            System.err.println("Bad URL string: " + webAddress + "\n"+ex);
        } catch (IOException ex) {
            System.err.println("Error reading stream: " + ex);
        } catch (URISyntaxException ex) {
            System.err.println("Erroneous URI syntax: " + ex);
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论