0

I am trying to test this code from oracle.com, but get the UnknownHostException. However I can easily go to oracle.com, and everything displays fine in the browser. There are no blocks or something on my computer.

Why does this happen?

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.oracle.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

EDIT

I appears I don't have any proxies . I checked like this

System.setProperty("java.net.useSystemProxies", "true");
System.out.println("detecting proxies");
List l = null;
try {
    l = ProxySelector.getDefault().select(new URI("http://foo/bar"));
} 
catch (URISyntaxException e) {
    e.printStackTrace();
}
if (l != null) {
    for (Iterator iter = l.iterator(); iter.hasNext();) {
        java.net.Proxy proxy = (java.net.Proxy) iter.next();
        System.out.println("proxy type: " + proxy.type());

        InetSocketAddress addr = (InetSocketAddress) proxy.address();

        if (addr == null) {
            System.out.println("No Proxy");
        } else {
            System.out.println("proxy hostname: " + addr.getHostName());
            System.setProperty("http.proxyHost", addr.getHostName());
            System.out.println("proxy port: " + addr.getPort());
            System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));
        }
    }
}


the result is 

detecting proxies
proxy type: DIRECT
No Proxy
Pasha
  • 181
  • 1
  • 1
  • 13
  • If there was a firewall issue, then i would not be able to open oracle.com with the browser right ? – Pasha Sep 18 '15 at 19:09
  • 3
    If your browser uses a proxy server, your machine doesn't need to know how to find machines on the internet. Are you on a managed network? – Peter Lawrey Sep 18 '15 at 19:12
  • yes, i'm on a managed network . how can i make this work – Pasha Sep 18 '15 at 19:16
  • 1
    Read this about [Java Networking and Proxies](https://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html) – Andreas Sep 18 '15 at 19:20
  • 1
    No, you wouldn't necessarily. *Local* (on-your-machine) firewalls can block specific, or all unknown, applications. Your browser might have permission that Java doesn't. – chrylis -cautiouslyoptimistic- Sep 18 '15 at 19:46
  • Look into [opening a connection with a Proxy object](http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#openConnection(java.net.Proxy)). I had this issue with our managed network at work. – Evan LaHurd Sep 18 '15 at 19:49

0 Answers0