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 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 and retrieves the IP from the response.

string URL = "";

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();


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.

Leave a Reply

Your email address will not be published. Required fields are marked *