Search code examples
javayahoo-financeurlconnection

Java URLConnection returns null for valid URL


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

Solution

  • 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.