xmlsocket java_java.net.SocketException - 尝试获取XML

我有一个简单的代码,从Web中获取XML文件 . 更具体地说,是一个来自挪威银行的汇率的XML文件 . 问题是,它有时只能起作用 . 通常,它永远不会第一次工作 .

经过一些测试,我很确定它在 InputStreamReader(url.openStream()) 上崩溃了 . 但正如所说,不是每一次 .

那么,是否有更好的方法来确保它正确地获取XML文件

所有帮助都是适用的 .

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());

}

}

错误如下:java.net.SocketException:在sun.security.ssl.InputRecord.readFully(java.net.SocketInputStream.read(未知来源)的java.net.SocketInputStream.read(未知来源)处重置连接 . 未知来源)sun.security.ssl.InputRecord.read(未知来源)sun.security.ssl.SSLSocketImpl.readRecord(未知来源)sun.security.ssl.SSLSocketImpl.waitForClose(未知来源)sun.security . ssl.HandshakeOutStream.flush(未知来源)at sun.security.ssl.Handshaker.kickstart(未知来源)sun.security.ssl.SSLSocketImpl.kickstartHandshake(未知来源)sun.security.ssl.SSLSocketImpl.performInitialHandshake(未知来源) )sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)at sun.security.ssl.SSLSocketImpl.startHandshake(未知来源)at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)at sun . net.net www.protocol.https.AbstractDelegateHttpsURLConnection.connect(未知来源)at sun.net.www.protocol.http.HttpURLConn sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源)的sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(未知来源)中的ection.getInputStream0(未知来源)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值