Support Forums
[C#] Get External IP through Webrequests [Intermediate] - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [C#] Get External IP through Webrequests [Intermediate] (/showthread.php?tid=11675)



[C#] Get External IP through Webrequests [Intermediate] - Mike - 08-31-2010

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



RE: [C#] Get External IP through Webrequests [Intermediate] - Fragma - 09-01-2010

Thanks this is very useful. Nice to see more C# threads as well.


RE: [C#] Get External IP through Webrequests [Intermediate] - Botted - 09-01-2010

yeah this looks good man, i'm going to try this.


RE: [C#] Get External IP through Webrequests [Intermediate] - Mike - 09-02-2010

(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


RE: [C#] Get External IP through Webrequests [Intermediate] - wchar_t - 09-02-2010

I already posted this.

:/


RE: [C#] Get External IP through Webrequests [Intermediate] - Mike - 09-02-2010

(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.


RE: [C#] Get External IP through Webrequests [Intermediate] - xHtmlPhP - 09-02-2010

Great to see a C# thread, although I'd probably use a WebClient for this matter.