我一直在收集一个项目的股市数据,并且在过去几个月中一直在这样做。但是,直到几天前,我一直使用的URLConnection从Yahoo下载数据为空,尽管没有进行任何更改并且在此之前运行良好。来自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,它对我有用。