我也嘗試使用GetIp方法(下)獲得互聯網IP得到正確的因特網IP,但只獲得一個IP這123.22.114.5,我重新啓動我的調制解調器很多次,但它仍然123.22.114.5,請幫助我。 (我使用Visual Studio 2010在Windows 7 64位)不能使用C#
(約我的英語不好對不起)
private string GetIp()
{
string extenalIp = "";
string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
string getIpRegex = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (Exception e)
{
// do some thing with exception
Console.WriteLine(e.Message);
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
if (m.Success)
{
extenalIp = m.Value.ToString();
}
wc.Dispose();
return extenalIp;
}
你需要獲得外部IP,這個問題有幾個解決方案:http://stackoverflow.com/questions/3253701/c-sharp-get-public-external-ip-address – 2012-07-29 08:06:36
是什麼讓你覺得123.22 .114.5不是您的互聯網IP嗎? – 2012-07-29 08:13:57
謝謝cheeseweasel – tainguyen 2012-07-29 08:16:05