GetYourWeightUp
GetYourWeightUp

Reputation: 145

Cant download JSON file from stats.nba using JSON.net

I want to download this json file http://stats.nba.com/stats/leaguestandingsv3?LeagueID=00&Season=2015-16&SeasonType=Regular+Season , but when I run my program nothing happends and I get a timeout exception.I have to eventually put it in a datatable so don't mind that part,I just want to get it to work first.I'm using json.net btw.

static void Main(string[] args)
    {
        DataTable dt1 = new DataTable();
        dt1.TableName = "winPCT";

        using (WebClient wc1 = new WebClient())
        {
            var json1 = wc1.DownloadString("http://stats.nba.com/stats/leaguestandingsv3?LeagueID=00&Season=2015-16&SeasonType=Regular+Season");
            var winPct = JsonConvert.DeserializeObject<RootObject>(json1);

            dt1.Columns.Add("Team");
            dt1.Columns.Add("WINS");
            dt1.Columns.Add("LOSSES");
            dt1.Columns.Add("WinPCT");
            foreach (var row in winPct.resultSets)
            {
                dt1.Rows.Add(row[2],//  2   Team name
                            row[12],//  12  WINS
                            row[13],//  13  LOSSES
                            row[14]);// 14  WinPCT

            }
        }
        dt1.WriteXml("winpct.xml");
    }

Upvotes: 1

Views: 448

Answers (1)

ザヘド
ザヘド

Reputation: 674

Adding the following headers to the request worked for me:

    using (WebClient wc1 = new WebClient())
    {
        var link = "http://stats.nba.com/stats/leaguestandingsv3?LeagueID=00&Season=2015-16&SeasonType=Regular+Season";
        wc1.Headers.Add("accept-encoding", "Accepflate, sdch");
        wc1.Headers.Add("Accept-Language","en");
        wc1.Headers.Add("origin","http://stats.nba.com");
        wc1.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
        var json1 = wc1.DownloadString(link);
    }

See the result here: https://dotnetfiddle.net/6Ugrm5

Upvotes: 4

Related Questions