我在公司网络中。在此网络中,我无法通过 IP ping 外部网站。我只能像浏览器一样通过 url 调用它们。这就是我使用 WebRequest 的原因。
当我尝试调用“www.google.com”时,我收到“无法解析远程名称 (www.google.com)”。
我的防火墙中没有“被阻止”的条目。使用此代码,我可以调用内部网站,但不能调用外部网站。
这是我的代码:
public void pingIP()
{
try
{
var url = uriBuilder;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.Uri);
//request.Accept = "*/*";
//request.UseDefaultCredentials = true;
//request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
//request.UserAgent = "Foo";
//request.Accept = "*/*";
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
_PingByURL = response.StatusCode == HttpStatusCode.OK;
}
catch
{
}
}
在 app.config 中:
<system.net>
<defaultProxy enabled ="true" useDefaultCredentials = "true">
<proxy usesystemdefault ="True" bypassonlocal="True"/>
</defaultProxy>
</system.net>
如何解析远程名称?这是检查网站是否可通过 URL 访问的最快速、最简洁的方法吗?
谢谢!
最佳答案
这没有问题!
static void Main(string[] args)
{
Console.WriteLine(PingTest());
}
public static bool PingTest()
{
Ping ping = new Ping();
PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"));
//For the web address you need !
//PingReply pingStatus = ping.Send("www.google.com");
return pingStatus.Status == IPStatus.Success;
}
可能存在网络限制,而您因此而失败。
关于c# - 无法解析远程名称 "www.google.com",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33493611/