2015-11-03 155 views
2

我在公司网络中。在这个网络我无法通过IP ping外部网站。我只能像浏览器那样通过url调用它们。这就是为什么我使用WebRequest远程名称无法解析“www.google.com”

当我尝试拨打“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访问的最快捷最简单的方法吗?

谢谢!

+0

我知道我有网络限制,我不能ping通谷歌。但我想通过WebResponse调用它来查看网站是否可用。 – Shadow

+0

从命令行刷新你的dns,看看是否有帮助:ipconfig/flushdns。您也可以运行tracert来查看它跳跃和阻止的位置......在dns处为10到1。 – 2015-11-03 08:32:08

+0

我冲洗了DNS。 DNS捕获是空的。我再次尝试,但它不工作。 – Shadow

回答

0

enter image description here这是我用ping通这对我的作品

public bool ConnectedToInternet() 
    { 
     var myPing = new Ping(); 
     const string host = "google.com"; 
     var buffer = new byte[32]; 
     const int timeout = 1000; 
     var pingOptions = new PingOptions(); 
     var reply = myPing.Send(host, timeout, buffer, pingOptions); 
     return reply != null && reply.Status == IPStatus.Success; 

    } 

试试这个,让我知道如果你没有问题的答复或不

+0

上的错误:var reply = myPing.Send(host,timeout,缓冲区,pingOptions); “主机未找到”;我尽量使用你的代码。 – Shadow

+0

@Shadow Iive编辑我的答案,告诉你这是工作 –

+0

我认为这是因为内部的DNS可能?仍然收到与“HostNotFound”相同的消息。 – Shadow

1

这是工作!

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; 

} 

这是可能的网络限制,你是因为他们失败。

+0

我同意这个作品,但如果你想使用这个答案,并正在使用一个URL删除'IPAddress.Parse'并更改一个URL的IP –

+0

@SimonPrice在这里我加了它的URL,你可以看到它! – mybirthname

+0

pingStatus.Status发生超时。我认为这是因为传出的ping被阻止。 – Shadow

相关问题