0

I'm trying to fetch info from an API, which returns a result in a JSON array.

This is my code:

string sURL = "http://api.planets.nu/games/list?limit=1";
WebRequest wrGetURL = WebRequest.Create(sURL);
StreamReader objReader = new StreamReader(wrGetURL.GetResponse().GetResponseStream());
string sLine;
sLine = objReader.ReadToEnd();
Console.WriteLine(sLine);

When pasting the URL in firefox, I get, in plain text:

[{"name":"Upikarium Sector","description":"This is a battle for the Upikarium Sector. This is a default configuration game. Custom map. This game has 2 turns per week.","shortdescription":"","status":2,"datecreated":"8\/28\/2011 10:11:28 PM","dateended":"1\/1\/0001 12:00:00 AM","maptype":2,"gametype":2,"wincondition":1,"difficulty":1.07299030764822,"tutorialid":0,"requiredlevelid":0,"maxlevelid":0,"masterplanetid":467,"quadrant":23,"mintenacity":0,"faststart":0,"turnsperweek":2,"yearstarted":8,"isprivate":false,"scenarioid":0,"createdby":"none","turn":229,"slots":11,"turnstatus":"xx3x_67x___","hostdays":"_M___F_","slowhostdays":"","hosttime":"18:3","lastbackuppath":"c:\\planetsdata\\backups\\game22158\\turn228-635191849885449557.zip","nexthost":"11\/8\/2013 6:03:00 PM","allturnsin":false,"lastnotified":false,"ishosting":false,"lastloadeddate":"11\/6\/2013 9:16:22 PM","deletedate":"","lasthostdate":"11\/4\/2013 6:04:49 PM","password":"","haspassword":false,"statusname":"Running","timetohost":"Next turn in 44 hours","id":22158}]

which is what I should get with my code. An array, with an object holding game info.

However, the result from the writeline is a bunch of garbled characters. I just can't seem to get proper textual results. I've been breaking my head over this for a while, and can't figure out why. It's probably has to do with the return being a JSON array, since a get call to an API returning a JSON dictionary results in perfectly readable text. This, however, refuses to be a readable string!

I'm pretty sure I'm missing something simple, but it just won't get to me!

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • did you try to parse it with json? – Noctis Nov 06 '13 at 22:12
  • Don't I need to get a readable result to parse? What I get is: ▼ ??zR ?]SMo?0♀?+??Yj?c?6♀?ð??<♦?M?D↔??&??>Jv?e?↕@O???#???pDY??‼=???(?b?←?k-M??f ·???♥?☺?▼Q?? ????r+??♫{?G/Z?{z?-?d?KnN?ys¶G?? ?☺?H???vbB+N?O[▬?♠c????z?u?j?ck??;~+??4?↕??Jj?? ¶????C?E?j??$I¤?N↕>?} ?(⌂?0&♫??.'??KG?♦????▲/|?&????,?↨y?2???H\-?H??3Y?F|?W???h? ◄4? ??~#?g?,h?↕2??????P?Cz♫a#???h↔;↨???6>??_$7Yza↨d???]l9?%?JX]|??R←?rI??????Rk? u☻?|??b⌂>∟♫∟0↑?;????=C▼♥??⌂▲V??????3FFn?☺?y??☼??u?,v8▲↔4???&♀&M?l??$M?wE?S?*?,wy ?Ru??X?XkT?*?u?pD?va??.????Wmy?WK?♦???V??]??!?A?_?Y? ?g??z????]?? -?\♀f??Ia????? ?☻????↕♦ – user2962485 Nov 06 '13 at 22:16
  • 1
    According to that comment, I close-voted for the wrong duplicate. This one is what you're looking for: [HttpWebRequest & Native GZip Compression](http://stackoverflow.com/questions/839888/httpwebrequest-native-gzip-compression). – CodeCaster Nov 06 '13 at 22:29

2 Answers2

4

As it states in the documentation, the content is gziped, add a reference to System.IO.Compression and use the following code:

 string sURL = "http://api.planets.nu/games/list?limit=1";
 HttpWebRequest wrGetURL = (HttpWebRequest)WebRequest.Create(sURL);
 wrGetURL.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
 StreamReader objReader = new StreamReader(wrGetURL.GetResponse().GetResponseStream(), Encoding.UTF8);
 string sLine;
 sLine = objReader.ReadToEnd();
 Console.WriteLine(sLine);
Lorentz Vedeler
  • 5,101
  • 2
  • 29
  • 40
  • Gah! I knew I overlooked something, thank you very much. That actually makes sense ... – user2962485 Nov 06 '13 at 22:27
  • Did not know about that property. It is defined in HttpWebRequest, though. Updated answer as that is a better approach. On another note; you probably don't want to write your own JSON-parser. – Lorentz Vedeler Nov 06 '13 at 22:56
1

It's zipped content. The easiest way to deal with this is by adding

wrGetURL.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

before you get the response.

Hope this helps,

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
Marvin Smit
  • 4,088
  • 1
  • 22
  • 21