Last active
July 16, 2018 06:42
-
-
Save xpunch/8c695504dc306e93332fae9179da2d7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IpLocation | |
{ | |
public string domain { get; set; } | |
public string address { get; set; } | |
public string numAddress { get; set; } | |
public string location { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('(', '[', ')', ']')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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