You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

读取ECB欧元汇率XML遇SSL证书错误,求可行解决方案

ECB汇率XML数据读取异常及解决方案需求

问题背景

之前可通过Java代码读取欧洲中央银行(ECB)提供的实时汇率XML数据,浏览器输入对应网址也能正常显示XML结构,但近期出现两类异常:

  • 代码抛出javax.net.ssl.SSLHandshakeException错误,具体提示:PKIX path building failed: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  • 浏览器输入该网址时,XML文件不再显示,而是直接自动下载到默认文件夹

需求

需实现以下两种方案之一:

  • 为Java配置访问ECB网址所需的有效证书
  • 通过编程方式触发该XML文件的下载,之后从本地文件读取数据

遇到的障碍

网上关于证书问题的解决方案大多绑定特定框架或库,尝试相关高赞方案时,第一步就卡壳:Firefox访问以xml结尾的地址时不显示锁图标,无法按步骤导出证书。

附加说明

本人极少在编程中处理网页访问,此前从未关注过证书相关问题。如果理解解决方案需要相关基础知识,欢迎提供教程或学习内容。

原代码

import java.io.*;
import java.net.*;

public class ReadURLTest {
    static String webAddress;

    public static void main(String[] args) {
        if (args.length==0)
            webAddress= "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.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);
        }
    }
}

内容的提问来源于stack exchange,提问作者Jörg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 17:10:16