This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Unfortunately, the above solution has some drawbacks. The hosts file can override the name to IP lookup and will also work in a disconnected mode. So for example, if I change my hosts file on c:\windows\system32\drivers\etc (note, there is another hosts file on c:\windows\i386) and and entry mapping www.gooogle.com to 184.108.40.206, the above code would return 220.127.116.11 even if my machine is unplugged.
I suggest using the following code to ascertain if your computer is connected to the network.
Of course you will have to made ammends for a proxy server, if any. Note that this is a blocking call and blocks for approx 5 seconds if no network is present.