SeanYang15
SeanYang15

Reputation: 51

Got java.net.UnknownHostException, can't ping any website, but can browse normally

I'm trying to write a program to send XML request via HTTP to a vendor server, and I used the example code from this link.

Then I got the error java.net.UnknownHostException when running the code Then I tried to ping the vendor host, and then www.google.com. None of them works. I got:

"ping request could not find host www.google.com"

I'm using corporate network. I can browse, and download and communicate to the vendor server with their web application normally. Any idea how to fix this?

Upvotes: 5

Views: 3400

Answers (3)

P Cherian
P Cherian

Reputation: 1

You are most likely behind a firewall.

Your firewall is preventing you to connect to internet using web scraping tools like Jsoup. Try connecting to a intranet site if you are in a corporate network.

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

You're able to browse the net without any problems because your browser must be configured to use a proxy. We can configure the JVM to use the same proxy and then open HTTP connections successfully.

Open your web browser's Network Settings and note down your Proxy Server and Port.

For Firefox go to Tools > Options > Advanced > Network > Connection > Settings

Now, in your Java program before you open the HTTP connection set up your JVM to use this Proxy.

System.getProperties().put("proxySet", "true");
System.getProperties().put("http.proxyHost", "10.1.0.11");
System.getProperties().put("http.proxyPort", "8080");

With the above properties set your program should be able to open connections. If your proxy needs authentication then you'd also have to setup a default Authenticator with a ProxyAuth object.

Ideally you should clear these properties before your program exits.

Upvotes: 3

rekire
rekire

Reputation: 47945

You should check the network settings of your system. You need to confirm that your have set a DNS server and a default gateway.

If in general your network configuration is correct you can try to use the command nslookup google.com 8.8.8.8. That will try to resolve the ip of google with one of their public dns servers.

Upvotes: 1

Related Questions