我正在Windows 8,Visual Studio 2012中开发Windows应用商店应用。我需要向特定的URL发出GET请求并获取JSON作为响应。我需要解析JSON以获取其中的值。我需要C#代码来执行上述功能。
答案 0 :(得分:7)
您可以使用HttpClient
课程。 GetAsync方法允许您将GET请求发送到指定的URL:
public async Task<JsonObject> GetAsync(string uri)
{
var httpClient = new HttpClient();
var content = await httpClient.GetStringAsync(uri);
return await Task.Run(() => JsonObject.Parse(content));
}
答案 1 :(得分:1)
您可以使用MSDN
中的示例代码 var client = new HttpClient();
var uri = new Uri("http://ponify.me/stats.php");
Stream respStream = await client.GetStreamAsync(uri);
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(rootObject));
rootObject feed = (rootObject)ser.ReadObject(respStream);
System.Diagnostics.Debug.WriteLine(feed.SONGHISTORY[0].TITLE);