How do i do to get my external IP-address in C# in the easiest way?
This post has been edited by johan_pirate: 04 March 2007 - 06:34 AM




Posted 04 March 2007 - 06:31 AM
This post has been edited by johan_pirate: 04 March 2007 - 06:34 AM
Posted 13 March 2007 - 10:45 PM
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName()); lblStatus.Text = "My IP address is " + IPHost.AddressList[0].ToString();
This post has been edited by shezzy: 13 March 2007 - 10:50 PM
Posted 21 March 2007 - 10:52 AM
shezzy, on 13 Mar, 2007 - 10:45 PM, said:
IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName()); lblStatus.Text = "My IP address is " + IPHost.AddressList[0].ToString();
Posted 22 March 2007 - 11:35 AM
Posted 13 April 2007 - 05:22 AM
aequasi, on 22 Mar, 2007 - 11:35 AM, said:
Posted 20 April 2007 - 04:34 PM
kmdshuaib, on 20 Apr, 2007 - 06:45 AM, said:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DreamInCode.Snippets
{
public class IpFinder
{
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
}
}
Posted 08 February 2008 - 12:45 PM
dkirkland, on 20 Apr, 2007 - 04:34 PM, said:
kmdshuaib, on 20 Apr, 2007 - 06:45 AM, said:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DreamInCode.Snippets
{
public class IpFinder
{
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
}
}
Posted 09 February 2008 - 05:05 AM
jayman9, on 8 Feb, 2008 - 03:21 PM, said:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DreamInCode.Snippets
{
public class IpFinder
{
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
}
}
This post has been edited by PsychoCoder: 09 February 2008 - 06:40 AM
Posted 09 February 2008 - 06:48 AM
class Form1
{
}
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
Posted 09 February 2008 - 09:28 AM
PsychoCoder, on 9 Feb, 2008 - 06:48 AM, said:
class Form1
{
}
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
Posted 09 February 2008 - 04:52 PM
kingmighty_spades, on 9 Feb, 2008 - 09:28 AM, said:
PsychoCoder, on 9 Feb, 2008 - 06:48 AM, said:
class Form1
{
}
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace getWanIp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
string baseurl = "http://checkip.dyndns.org/";
Stream data = client.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString();
MessageBox.Show(s);
}
}
}
Posted 25 December 2008 - 11:14 PM
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
IPAddress externalIp = IPAddress.Parse(requestHtml);
return externalIp;
}
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
