Skip to content

Instantly share code, notes, and snippets.

@archer884
Created May 7, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archer884/f25402af7cccb6bb190e to your computer and use it in GitHub Desktop.
Save archer884/f25402af7cccb6bb190e to your computer and use it in GitHub Desktop.
Checks zippopotam.us to see if a zip code is valid
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace ZC
{
class Program
{
static void Main(string[] args)
{
using (var client = new WebClient())
{
if (args.Length == 0)
{
args = new[] { "79088" };
}
try
{
var query = String.Format("http://www.zippopotam.us/us/{0}", args[0]);
var result = JsonConvert.DeserializeObject<QueryResult>(client.DownloadString(query));
Console.WriteLine("{0}, {1}", result.Places.Single().PlaceName, result.Places.Single().State);
}
catch (WebException e)
{
if ((e.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine("Invalid zip");
}
else
{
Console.WriteLine("Unable to serve your request");
}
}
}
}
}
public class Place
{
[JsonProperty("place name")]
public string PlaceName { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
public string State { get; set; }
[JsonProperty("state abbreviation")]
public string StateAbbreviation { get; set; }
}
public class QueryResult
{
[JsonProperty("post code")]
public string PostCode { get; set; }
public string Country { get; set; }
[JsonProperty("country abbreviation")]
public string CountryAbbreviation { get; set; }
public ICollection<Place> Places { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment