Пример #1
0
        static CarMakesResponse CarMakesRequest(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format(
                                                "Server error (HTTP {0}: {1}).",
                                                response.StatusCode,
                                                response.StatusDescription));
                    }

                    StreamReader streamReader = new StreamReader(response.GetResponseStream());
                    string       responseText = streamReader.ReadToEnd();
                    string       responseJson = JsonTrim(responseText);

                    JsonSerializer   jsonSerializer   = new JsonSerializer();
                    CarMakesResponse carMakesResponse = JsonConvert.DeserializeObject <CarMakesResponse>(responseJson);
                    return(carMakesResponse);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            CarMakesResponse carMakesResponse = CarMakesRequest(CarMakesUrl);
            Dictionary <string, CountryCarMakeCount> carMakeCounts = CollateMakes(carMakesResponse.Makes);

            if (TestCollateMakes(carMakesResponse.Makes, carMakeCounts.Values.ToArray <CountryCarMakeCount>()))
            {
                Console.WriteLine("Car make data was successfully collated into counts of each country's common and uncommon makes");
            }
            else
            {
                Console.WriteLine("Collated country make data doesn't match general car make data");
            }
            Console.ReadLine();

            try
            {
                string json = JsonConvert.SerializeObject(carMakeCounts.Values);
                File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\makeCounts.json", json); //Writes to user's Documents folder
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }