【问题标题】:The remote name could not be resolved "www.google.com"无法解析远程名称“www.google.com”
【发布时间】:2015-11-03 07:29:47
【问题描述】:

我在公司网络中。在此网络中,我无法通过 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 访问的最快速、最简洁的方法吗?

谢谢!

【问题讨论】:

  • 我知道我有网络限制,无法 ping 谷歌。但我想通过 WebResponse 调用它以查看该网站是否可用。
  • 从命令行刷新你的 dns 看看是否有帮助:ipconfig /flushdns。您还可以运行 tracert 以查看它在哪里跳跃和阻塞...在 dns 上 10 比 1。
  • 我刷新了 dns。 dns 捕获为空。我又试了一次,还是不行。
  • 使用此代码,我从内部服务器获得了 WebResponse,如果我登录并使用外部 Internet,我可以 ping 外部服务器,因为它不使用 proxy.pac

标签: c# dns httpwebrequest httpwebresponse


【解决方案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;

}

可能有网络限制,您因此而失败。

【讨论】:

  • 我同意这行得通,但如果您想使用此答案并使用 url 删除 IPAddress.Parse 并更改 url 的 ip
  • @SimonPrice 这里我加了url,你可以看到!
  • pingStatus.Status 超时。我认为这是因为传出的 ping 被阻止了。
  • 仍然使用新行我得到“HostNotFound”
  • 没有。我无法ping通外部IP。我只能通过 http 请求调用它们。
【解决方案2】:

this 是我用来 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;

    }

试试这个,如果你得到回复,请告诉我

【讨论】:

  • 错误:var reply = myPing.Send(host, timeout, buffer, pingOptions); “主机未找到”;我试试你的代码。
  • @Shadow ive 刚刚编辑了我的答案,向您展示这是有效的
  • 我认为可能是因为内部 dns?仍然收到与“HostNotFound”相同的消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2016-05-31
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多