Skip to content

Instantly share code, notes, and snippets.

@xpunch
Last active July 16, 2018 06:42
Show Gist options
  • Save xpunch/8c695504dc306e93332fae9179da2d7d to your computer and use it in GitHub Desktop.
Save xpunch/8c695504dc306e93332fae9179da2d7d to your computer and use it in GitHub Desktop.
public class IpLocation
{
public string domain { get; set; }
public string address { get; set; }
public string numAddress { get; set; }
public string location { get; set; }
}
var response = await httpClient.PostAsync("http://ip.chinaz.com/ajaxsync.aspx?at=ip", new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("ip", ip)
}));
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IpLocation>(result.Trim('(', '[', ')', ']'));
var file = new FileInfo();
using (var package = new ExcelPackage(file))
{
var workBook = package.Workbook;
var worksheet = workBook.Worksheets.First();
var cells = worksheet.Cells.Value as object[,];
if (null == cells)
{
Console.WriteLine("failed");
return;
}
var cache = new Dictionary<string, IpLocation>();
for (var index = 1; index < cells.GetLength(0); index++)
{
if (null == worksheet.Cells[index, 0].Value || null != worksheet.Cells[index, 3].Value) continue;
try
{
var ip = worksheet.Cells[index, 0].Value.ToString();
var location = cache.ContainsKey(ip) ? cache[ip] : GetLocation(httpClient, ip).Result;
worksheet.Cells[index, 2].Value = location.numAddress;
worksheet.Cells[index, 3].Value = location.location;
Console.WriteLine($"Finish {ip}");
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
package.Save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment