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

时间:2015-11-03 07:29:48

标签: c# dns httpwebrequest httpwebresponse

我在公司网络中。在此网络中我无法通过IP ping外部网站。我只能像浏览器一样通过URL调用它们。这就是为什么我使用 WebRequest

当我尝试致电" www.google.com"我得到了一个无法解析的远程名称(www.google.com)"。

我没有"被阻止"进入我的防火墙。使用此代码,我可以调用内部网站,但不能访问外部网站。

这是我的代码:

a = b = [] # a -> [] <- b
b.append([1, 2, 3]) # a -> [[1, 2, 3]] <- b

在app.config中:

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
        {

        }

    }

如何解析远程名称?这是检查网站是否可以通过URL访问的快速而最干净的方法吗?

谢谢!

2 个答案:

答案 0 :(得分: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;

}

可能存在网络限制,但由于它们而失败。

答案 1 :(得分: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;

    }

尝试此操作,如果您收到回复,请告诉我

相关问题