2015-12-22 11 views
8

Napisałem mały program do analizy danych mojego profilu z API StackExchange, ale api zwraca mi dane nieparujące/nieczytelne.Nieodparta odpowiedź API StackExchange

Dane otrzymane: (samo pobierać za pomocą C#)

\ u001f \ b \ 0 \ 0 \ 0 0 \ 0 \ \ u0004 \ 0mRMo0 \ f /: d $ c˱ '^{/ \ u0006 \ u0018G > \ I \ u0015 \ u0004 ݀ D> GR L' o \ u0004 G % JP \ u001c - Em> 0 X bm ~ \ u0014 M] r dLG v0 ~ Fj = 1 \ u00031I > kTRA \ "(/+. ;Nl\u0018 ?h \u0014 P 藄 X aL w # 3 \ u0002 + \ u007f \ u0010 \ u000f p ] v \ u007f \ t ڧ \ nf "\ u0018 \ u0014eƺ _ 1x # j^- c AX \ t \ u001aT @ qj \ u001aU7 \ u0014 \ "\ a^\ b # \ u001e QG % y \ t ח q00K \ av \ u0011 {ظ \ u0005 \ "\ u001d + | \ u007f ' \ u0016 ~ 8 \ u007f \ U0001-h] O \ u007fVo \ u007f \ U0001 ~ Y \ u0003 \ u0002 \ 0 \ 0

Dane chciał: (copy-wklejane z mojej przeglądarce)

{"items": [{"badge_counts", {"bronze": 987, "silver": 654, "gold": 321}, "account_id": 123456789, "is_employee": false, "last_modified_date": 1250612752 , "last_access_date": 1250540770, "age": 0, "reputation_change_year": 987, "reputation_change_quarter": 654, "reputation_change_month": 321, "reputation_change_week": 98, "reputation_change_day": 76, "reputation": 9876, " creatio n_data ": 1109670518," user_type ":" registered "," user_id ": 123456789," accept_rate ": 0," location ":" Australia "," website_url ":" http://example.org "," link ":" http://example.org/username "," profile_image ":" http://example.org/username/icon.png”, "DISPLAY_NAME": "nazwa użytkownika"}], "has_more" false "quota_max": 300, "quota_remaining": 300}

pisałem tę metodę (rozbudowa) pobrać ciąg z internetu:

public static string DownloadString(this string link) 
{ 
    WebClient wc = null; 
    string s = null; 
    try 
    { 
     wc = new WebClient(); 
     wc.Encoding = Encoding.UTF8; 
     s = wc.DownloadString(link); 
     return s; 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
    finally 
    { 
     if (wc != null) 
     { 
      wc.Dispose(); 
     } 
    } 
    return null; 
} 

ja potem szukałem w internecie i znalazłem metodę pobierania sznurki, za pomocą kilku innych taktyk:

public string DownloadString2(string link) 
{ 
    WebClient client = new WebClient(); 
    client.Encoding = Encoding.UTF8; 
    Stream data = client.OpenRead(link); 
    StreamReader reader = new StreamReader(data); 
    string s = reader.ReadToEnd(); 
    data.Close(); 
    reader.Close(); 
    return s; 
} 

Jednak obie metody zwracają takie same dane (nieprzeczytane/nieodwracalne).

Jak uzyskać czytelne dane z interfejsu API? Czy czegoś brakuje?

+0

FWIW, 'try {} finally {}' bloki bez 'catch {}' bloki są całkowicie poprawny, jeśli wszystko, co robisz, to ponowne wyrzucenie wyjątku. –

+0

Jaki jest adres URL, z którego uzyskujesz dostęp? –

+0

@PatrickHofman http://api.stackexchange.com/2.2/users/4000926?site=stackoverflow – cramopy

Odpowiedz

9

Wydaje mi się, że dane wyjściowe są skompresowane. Możesz użyć GZipStream, który można znaleźć w System.IO.Compression w celu rozpakowania bajtów.

public static string DownloadString(this string link) 
{ 
    WebClient wc = null; 
    try 
    { 
     wc = new WebClient(); 
     wc.Encoding = Encoding.UTF8; 
     byte[] b = wc.DownloadData(link); 

     MemoryStream output = new MemoryStream(); 
     using (GZipStream g = new GZipStream(new MemoryStream(b), CompressionMode.Decompress)) 
     { 
      g.CopyTo(output); 
     } 

     return Encoding.UTF8.GetString(output.ToArray()); 
    } 
    catch 
    { 

    } 
    finally 
    { 
     if (wc != null) 
     { 
      wc.Dispose(); 
     } 
    } 
    return null; 
} 
1

jak widać - kodowanie uzywasz jest źle -

enter image description here

Powiązane problemy