public async Task <string> GetInfomationsOfStationByNameAsync(string nameOfCity, string numOfStation) { DateTime start = DateTime.Now; MonitorStat.AddRequestFromClient(); if (cacheOfStation[numOfStation] != null) { MonitorStat.AddDelay(DateTime.Now.Subtract(start).TotalMilliseconds); return((string)cacheOfStation[numOfStation]); } request = WebRequest.Create( "https://api.jcdecaux.com/vls/v1/stations/" + numOfStation + "?contract=" + nameOfCity + "&apiKey=3857a4c9c72e34c322bd73cd36dec39dd7d15dd9"); Console.WriteLine(nameOfCity + numOfStation); try { response = await request.GetResponseAsync(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. reader = new StreamReader(dataStream); // Read the content. MonitorStat.AddRequestToVelib(); responseFromServer = reader.ReadToEnd(); if (responseFromServer.Length > 50) { RootObject rb = JsonConvert.DeserializeObject <RootObject>(responseFromServer); cacheOfStation.Set(numOfStation, rb.ToString(), cacheItemPolicy); MonitorStat.AddCacheInfo(); MonitorStat.AddDelay(DateTime.Now.Subtract(start).TotalMilliseconds); return(rb.ToString()); } else { MonitorStat.AddDelay(DateTime.Now.Subtract(start).TotalMilliseconds); return("Not Found!"); } } catch (WebException wex) { Console.WriteLine("Web Exception" + wex); MonitorStat.AddDelay(DateTime.Now.Subtract(start).TotalMilliseconds); return("Not Found!"); } }
public async Task <string> GetAllStationsByCityAsync(string nameOfCity) { DateTime start = DateTime.Now; MonitorStat.AddRequestFromClient(); if (cacheOfCity[nameOfCity] != null) { MonitorStat.AddDelay(DateTime.Now.Subtract(start).TotalMilliseconds); return((string)cacheOfCity[nameOfCity]); } string res = ""; request = WebRequest.Create( "https://api.jcdecaux.com/vls/v1/stations?contract=" + nameOfCity + "&apiKey=3857a4c9c72e34c322bd73cd36dec39dd7d15dd9"); try { response = await request.GetResponseAsync(); // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. reader = new StreamReader(dataStream); MonitorStat.AddRequestToVelib(); // Read the content. responseFromServer = reader.ReadToEnd(); List <RootObject> rb = JsonConvert.DeserializeObject <List <RootObject> >(responseFromServer); foreach (RootObject ob in rb) { res = res + ob.name + "\n"; } cacheOfCity.Set(nameOfCity, res, cacheItemPolicy); MonitorStat.AddCacheInfo(); MonitorStat.AddDelay(DateTime.Now.Subtract(start).TotalMilliseconds); } catch (WebException wex) { MonitorStat.AddDelay(DateTime.Now.Subtract(start).TotalMilliseconds); Console.WriteLine("Web Exception" + wex); } return(res); }
private async Task <JObject> GetArray(string requestUrl) { // Create a request for the URL. WebRequest request = WebRequest.Create(requestUrl); // If required by the server, set the credentials. // request.Credentials = CredentialCache.DefaultCredentials; // Get the response. WebResponse response = await request.GetResponseAsync(); // Display the status. // Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. MonitorStat.AddRequestToVelib(); string responseFromServer = reader.ReadToEnd(); // Display the content. return(JObject.Parse(responseFromServer)); }