Created
November 15, 2016 07:01
-
-
Save twinkfrag/0d89bade9328350d1ce3c13e01806581 to your computer and use it in GitHub Desktop.
IwiNetPro16ApiClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PM> Install-Package Newtonsoft.Json | |
// Add Reference to System.Web; | |
using System; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web; | |
using Newtonsoft.Json; | |
namespace IwiNetPro16ApiClient | |
{ | |
public class ApiClient | |
{ | |
static void Main(string[] args) | |
{ | |
var task = new ApiClient().RequestGetWeather(130010); | |
// 本当なら | |
// var resJson = await new ApiClient().RequestGetWeather(130010); | |
// と書きたいところだがMainはasyncじゃないので書けない | |
task.Wait(); | |
var resJson = task.Result; | |
Console.WriteLine($"{resJson.location.city}の天気は{resJson.forecasts[0].telop}です."); | |
Console.ReadKey(); | |
} | |
public async Task<dynamic> RequestGetWeather(int locationCode) | |
{ | |
var query = HttpUtility.ParseQueryString(string.Empty); | |
query["city"] = locationCode.ToString(); | |
var uri = new UriBuilder | |
{ | |
Scheme = "http", | |
Host = "weather.livedoor.com", | |
Path = "forecast/webservice/json/v1", | |
Query = query.ToString(), | |
}.Uri; | |
using (var wc = new WebClient()) | |
{ | |
var data = await wc.DownloadDataTaskAsync(uri); | |
var json = JsonConvert.DeserializeObject<dynamic>(Encoding.UTF8.GetString(data)); | |
return json; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment