c# - 从网站获取字符串

标签 c#

您好,我正在尝试编写一个程序,我可以从 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/

相关文章:

c# - 如何从头开始创建声音 C#

c# - Fizz buzz 程序奇怪的输出?

c# - 在设计时设置 ComboBox 的 Text 和 Value 属性

c# - WCF 服务 - 从 https 迁移到 http

c# - cefsharp 和 previewkeydown 事件不起作用

c# - 此方法按钮C#中的语法错误?

c# - Azure 服务管理 Api 配置更改(400 错误请求错误)

c# - OpenPop 不再下载消息 UID

C# Outlook - 调用被被叫方拒绝

javascript - $.ajax 成功 : value is always undefined