Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 332
SeanYang15 Avatar asked Oct 05 '22 00:10

SeanYang15


2 Answers

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.

like image 105
Ravi K Thapliyal Avatar answered Oct 13 '22 12:10

Ravi K Thapliyal


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.

like image 32
rekire Avatar answered Oct 13 '22 11:10

rekire