Support Forums

Full Version: [C#] Get External IP through Webrequests [Intermediate]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is poorly written but does the job. Didn't bother with timeouts, which probably need to be added. And there probably is a faster way of getting your IP but this is the fastest way I could think of.

Imports:
Code:
using System.Net;
using System.IO;

Code:
Code:
static string IP()
        {
            //http://www.whatismyip.com/automation/n09230945.asp
            HttpWebRequest WhatIsMyIP_Request = (HttpWebRequest)HttpWebRequest.Create("http://www.whatismyip.com/automation/n09230945.asp");
            HttpWebResponse WhatIsMyIP_Response = (HttpWebResponse)WhatIsMyIP_Request.GetResponse();
            StreamReader WhatIsMyIP_Source = new StreamReader(WhatIsMyIP_Response.GetResponseStream());
            return WhatIsMyIP_Source.ReadToEnd();
        }

Usage:
Code:
Console.WriteLine(IP());
Thanks this is very useful. Nice to see more C# threads as well.
yeah this looks good man, i'm going to try this.
(09-01-2010, 02:07 PM)Fragma Wrote: [ -> ]Thanks this is very useful. Nice to see more C# threads as well.

I'm glad. Thank you. Thumbsup
I already posted this.

:/
(09-02-2010, 07:06 AM)Scammer Wrote: [ -> ]I already posted this.

:/

Two different snippets, not the matter of the output (actually yes..), but the matter of the code. Great minds think alike.
Great to see a C# thread, although I'd probably use a WebClient for this matter.