Getting your dynamic public IP in C# & .net
For one of my projects I required the dynamic public of my ISP connection. I blogged earlier about getting this through www.network-tools.com but in this case I needed it in my script. I wrote a quick script to scrape it of the same site.
This code makes a HTTP request to network-tools.com and retrieves the IP from the response.
string URL = "http://www.network-tools.com";
HttpWebRequest HWR = (HttpWebRequest)HttpWebRequest.Create(URL);
HWR.Method = "GET";
StreamReader SR = new StreamReader(HWR.GetResponse().GetResponseStream());
string Response = SR.ReadToEnd();
string Pattern = @"dd?d?.dd?d?.dd?d?.dd?d?";
Regex R = new Regex(Pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
Match M = R.Match(Response);
string IP = M.ToString();
SR.Close();
HttpWebRequest and HttpWebResponse are two very useful classes in .NET. I will recommened reading up on this and I will soon blog about them again.
Related Posts
- Mixing Assembly language with Visual Basic
- Introduction to Face Detection and Face Recognition
- Hardware Platforms for Embedded Computer Vision, Image Processing and Deep Learning
- Do all versions of OpenCV run at the same speed?
- Introduction to OpenCV
- Best Concise Linux System Administrator’s Guide (SAG)
- Dynamic DNS update from .C# and .net
- Draw graphs using OpenCV
- To RAID or not to RAID
- Setting up IIS 7.5 and Apache on same server