gpt4 book ai didi

Java URLConnection 对于有效 URL 返回 null

转载 作者:行者123 更新时间:2023-12-02 02:53:15 25 4
gpt4 key购买 nike

过去几个月我一直在为一个项目收集股票市场数据。然而,截至几天前,我用来从雅虎下载数据的 URLConnection 已经为空,尽管没有进行任何更改并且在此之前它一直运行良好。来自 Yahoo 的 URL 下载一个 csv 文件,我读取并打印该文件,如果我在浏览器上访问该 URL,该 URL 是有效的并且可以工作,但是当我在代码中连接到它时, URLConnection 为 null。尝试其他股票也没有帮助。

private URL url = null;
private URLConnection urlConn = null;
private InputStreamReader inStream = null;
try {
//http://ichart.finance.yahoo.com/table.csv?s=^FTSE&d=3&e=18&f=2017&g=d&a=1&b=14&c=2017
this.url = new URL(urlStr);
System.out.println(url.toString());
this.urlConn = url.openConnection();
this.urlConn.connect();

//Start Reading
this.inStream = new InputStreamReader(this.urlConn.getInputStream());
BufferedReader buff= new BufferedReader(this.inStream);
System.out.println(buff.readLine());
}catch (MalformedURLException e) {
System.out.println(e.getMessage());
}catch(IOException e){
System.out.println(e.getMessage());
}

最佳答案

使用curl显示URL已重定向到https。

$ curl -v -H "Content-Type: text/csv" "http://ichart.finance.yahoo.com/table.csv?s=^FTSE&d=3&e=18&f=2017&g=d&a=1&b=14&c=2017"
* Trying 98.139.199.204...
* Connected to ichart.finance.yahoo.com (98.139.199.204) port 80 (#0)
> GET /table.csv?s=^FTSE&d=3&e=18&f=2017&g=d&a=1&b=14&c=2017 HTTP/1.1
> Host: ichart.finance.yahoo.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: text/csv
>
< HTTP/1.1 301 Moved Permanently
< Date: Wed, 19 Apr 2017 02:48:29 GMT
< Via: http/1.1 media-router68.prod.media.bf1.yahoo.com (ApacheTrafficServer [c s f ]), http/1.1 r23.ycpi.bf1.yahoo.net (ApacheTrafficServer [cMsSfW])
< Server: ATS
< Location: https://ichart.finance.yahoo.com/table.csv?s=^FTSE&d=3&e=18&f=2017&g=d&a=1&b=14&c=2017
< Content-Length: 0
< Age: 0
< Connection: keep-alive
<
* Connection #0 to host ichart.finance.yahoo.com left intact

更改您的 URL 以使用 https 而不是 http。我进行了更改,在代码中的 URL 中使用 https,它对我有用。

关于Java URLConnection 对于有效 URL 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com