using System.Collections.Generic;
using System.Text.RegularExpressions;
public static void Main()
Console.WriteLine(PhoneRegionExtractor.GetRegion(9173006556));
public static class PhoneRegionExtractor
private static Dictionary<long, string> _cache;
public static string GetRegion(long phone)
throw new ApplicationException("Invalid phone number");
string result = GetOrAddFromCache(_cache, phone);
private static bool validate(long phone)
Regex Rex = new Regex("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]");
return Rex.IsMatch(phone.ToString());
private static string GetFromWebservice(long phone)
var regex = new Regex("(\"region\".*\"name\":\"(?<region>.*)\",\"okrug\")");
var client = new WebClient();
string s = client.DownloadString("https://htmlweb.ru/json/mnp/phone/" + phone);
var match = regex.Match(s);
return match.Success ? match.Groups["region"].Value : string.Empty;
private static string GetOrAddFromCache(Dictionary<long, string> cache, long number)
cache = new Dictionary<long, string>();
if (cache.ContainsKey(number))
string result = GetFromWebservice(number);
cache.Add(number, result);