views:

881

answers:

6

Try as I might, I'm unable to resolve an address to IP. The code snippet is shown below. I keep getting the No such host is known exception, even though I could access google with my browser (The DNS server is almost certainly working). I'm however behind company's firewall.

try
{
   foreach (IPAddress address in Dns.GetHostAddresses("www.google.com"))
   {
      Console.WriteLine(address.ToString());
   }
}
catch (SocketException e)
{
   Console.WriteLine("Source : " + e.Source); // System
   Console.WriteLine("Message : " + e.Message); // No such host is known
}
+2  A: 

There is nothing wrong with your code. Given that you can access www.google.com from a web browser the next most likely problem is that the web browser is using a proxy server. The web browser is actually accessing www.google.com through the proxy server which is allowed through the firewall. The simple application you wrote is not allowed through the firewall and is resulting in an exception.

You can verify this by looking at the proxy settings in Internet Explorer.

Tools -> Options -> Connections -> Lan Settings

There will be a proxy server group of settings. If there is a value present, this is almost certainly your problem.

JaredPar
IP resolution is usually done within the firewall, however. He's not actually trying to connect to www.google.com on the code. My guess is that the proxy is doing the DNS lookup, and he just needs to reconfigure DNS locally.
Jon Skeet
I've seen it done both ways. I've worked places where the firewall or other network security infrastructure will block DNS if it's not going through a proxy service of sorts.
JaredPar
A: 

Yes, I do have proxy server settings. Will it be possible to made that piece of C# code to work, regardless of the PC's proxy server settings?

I don't think so, you could theoretically grab IE preferences though.
Luk
A: 

Rather than try through a browser, try pinging www.google.com (or some other host, of course) from the command line.

The ping itself may well not work, but it should show the IP address resolution first. If you get an error message like this:

    Ping request could not find host www.google.com.
    Please check the name and try again.

then it's likely that the proxy server is doing the DNS lookup for you when you're browsing, and your DNS server is either not working or your machine's network settings are incorrect.

Jon Skeet
+1  A: 

You need to set up the proxy:

here's a snippet that should set it up for all the following calls:

    protected void SetupProxy(string proxyUrl, string proxyLogin, string proxyPassword, string[] proxyBypass)
    {
        WebProxy proxy = new WebProxy(proxyUrl);
        proxy.Credentials = new NetworkCredential(proxyLogin, proxyPassword);
        proxy.BypassList = proxyBypass;
        proxy.BypassProxyOnLocal = true;
        WebRequest.DefaultWebProxy = proxy;
    }
Luk
A: 

Can you lookup ip address of www.google.com by command line (nslookup or ping) ?

Fuangwith S.
A: 

I could not ping www.google.com in command line. I get the "Ping request could not find host www.google.com. Please check the name and try again."