c# - 无法解析远程名称 "www.google.com"

标签 c# dns httpwebrequest httpwebresponse

我在公司网络中。在此网络中,我无法通过 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/

相关文章:

c# - 交易范围和手动定义的交易之间的区别?

c# - 接口(interface)在内部是如何工作的?

go - 使用 Kubernetes 集群 k8s 中的 SRV 记录通过 Golang 中的 DNS 实现对等发现逻辑

automation - 有没有用于网页自动获取的宏记录器?

c# - 为什么 asyncController 无法获取数据

c# - 在子类的构造函数中调用基构造函数

linux - Centos需要很长时间才能解析本地网络附近的服务器

php - 如何查询特定的名称服务器( native php)

httpwebrequest - .NET HttpClient在多个请求后挂起(除非Fiddler处于事件状态)

c# - 使用 HttpWebRequest/Response 构建 .NET REST 客户端有什么缺点吗