您好,我正在尝试编写一个程序,我可以从 http://ifconfig.me/ip 获取我的公共(public) IP 地址。
但我的代码失败了..程序编译但是当我按下按钮获取 ip 并将其显示在 txtbox 上时程序被卡住我已经尝试了以下代码
using system.xml;
XmlDocument document = new XmlDocument();
document.Load("http://ifconfig.me/ip");
string allText = document.InnerText;
我的其他代码是
using system.net
WebClient abc = new WebClient();
txtMyIp.Text = abc.DownloadString("http://www.ifconfig.me/ip");
谁能告诉我为什么当我按下按钮时程序会死机?或者建议我任何其他方式/方法?
顺便说一句,我正在使用 visual studio 2012
最佳答案
是网站导致的。它只是需要很长时间才能加载。
您应该尝试使用另一个网站:http://wtfismyip.com/text
我试过了
string myIP = new WebClient().DownloadString("http://wtfismyip.com/text");
而且效果很好。
关于c# - 从网站获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23814727/