private async void FetchAddressBtn_Click(object sender, EventArgs e)
{
FetchAddressBtn.Enabled = false;
StatusText.Show();
StatusPic.Show();
IPAddress addy = null;
// WebClient can sometimes cause the GUI to lock being all slow and such, so task it up
await Task.Run(() =>
{
// Use a web client to fetch our IP Address
using (WebClient Web = new WebClient())
{
// Disable Proxy to prevent slowness
Web.Proxy = null;
// Set headers (Copied from my current Chrome headers)
Web.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36";
Web.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
Web.Headers["Accept-Language"] = "en-US,en;q=0.8";
// Loop through each service and check for our IP
for (int i = 0; i < IpServices.Length; i++)
{
try
{
// Attempt to Fetch the IP address from this service
string result = Web.DownloadString(IpServices[i]);
Match match = Regex.Match(result, @"(?<IpAddress>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})");
if (match.Success && IPAddress.TryParse(match.Groups["IpAddress"].Value, out addy))
{
break; // Success, we have an IP so stop here
}
}
catch
{
continue; // Error, Skip to next service
}
}
}
});
// If we were unable to fetch the IP address, then alert the user
if (addy == null)
{
StatusText.Text = "Unable to fetch external address!";
StatusPic.Image = BF2Statistics.Properties.Resources.error;
}
else
{
StatusText.Text = "Address Fetched Successfully!";
StatusPic.Image = BF2Statistics.Properties.Resources.check;
AddressTextBox.Text = addy.ToString();
}
}
}