Java URLConnection returns null for valid URL

*爱你&永不变心* 提交于 2019-12-12 04:33:38

问题


I've been collecting stock market data for a project, and have been doing so for the past few months. However, as of a couple of days ago the URLConnection I've been using to download data from Yahoo has been null, despite having made no changes and it having worked perfectly before that. The URL from Yahoo downloads a csv file which I read and print out, and the URL is valid and works if I go to it on my browser, yet when I connect to it in my code, the URLConnection is null. Trying with other stocks also hasn't helped.

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

回答1:


Using curl shows that the URL has been redirected to 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

Change your URL to use https instead of http. I made the change to use https in the URL within your code and it worked for me.



来源:https://stackoverflow.com/questions/43485297/java-urlconnection-returns-null-for-valid-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!