Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C#] Get External IP through Webrequests [Intermediate]
#1
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());
Reply
#2
Thanks this is very useful. Nice to see more C# threads as well.
Reply
#3
yeah this looks good man, i'm going to try this.
[Image: 2laa4ac.png]
Reply
#4
(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
Reply
#5
I already posted this.

:/
Reply
#6
(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.
Reply
#7
Great to see a C# thread, although I'd probably use a WebClient for this matter.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [C#] Simple RC2 Encryption/Decryption [Intermediate] Mike 3 3,414 12-21-2010, 11:38 PM
Last Post: ZxPwn420
  [C#] Loading/Writing XML Files [Intermediate] Mike 4 2,061 09-29-2010, 10:17 AM
Last Post: Fitz-
  [C#] Get External IP. wchar_t 3 1,706 08-14-2010, 03:57 PM
Last Post: wchar_t

Forum Jump:


Users browsing this thread: 1 Guest(s)