2014-09-02 59 views
0

我想連接到堆棧溢出API作爲我的第一個API調用之一,但我掙扎。連接到堆棧溢出API

有人能告訴我爲什麼這段代碼沒有返回成功代碼嗎?

using (var handler = new HttpClientHandler()) 
using (var client = new HttpClient(handler)) 
{ 
      client.BaseAddress = new Uri("https://api.stackexchange.com/"); 
      var response = await client.GetAsync("questions"); 

      if (response.IsSuccessStatusCode) 
      { 
      } 
      else 
      { 
       Console.WriteLine(response.ToString()); 
      } 
} 
+1

它返回什麼代碼? – xDaevax 2014-09-02 18:46:28

+0

您是否嘗試過在瀏覽器中點擊「http:// api.stackexchange.com/questions」?它給你一個400 – Jonesopolis 2014-09-02 18:49:01

+0

'{「error_id」:400,「error_message」:「site is required」,「error_name」:「bad_parameter」}' – jball 2014-09-02 18:49:28

回答

0

響應告訴你site是必需的。改爲打https://api.stackexchange.com/questions?site=stackoverflow

0

如果你讀了錯誤和文檔你找回

{"error_id":400,"error_message":"site is required","error_name":"bad_parameter"} 

,它需要知道你想要的StackExchange網站。嘗試:

https://api.stackexchange.com/questions?site=stackoverflow