5

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?

2
  • Your coroporate network probably has no DNS for external domains, and the resolution is done via a proxy. So, no DNS for you! And you need to go through a proxy to do your HTTP stuff. Commented May 28, 2013 at 19:45
  • You should really take a look at Spring and JaxB, way easier approach then the demo in that tutorial. Google about Spring's RestTemplate and how to marshall/unmarshall with JaxB. Commented May 28, 2013 at 20:01

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Ravi and @fge You are correct. The browser is using the proxy setting done on the company computer, and it's using a automatic configuration script. Is there a way to have the program using the scirpt as well? Since I see multiple hosts for different situations in the script and I'm not sure which one I should use here.
On another note: Since the first part of the proxy script has "if ((host == "127.0.0.1") || (host == "localhost")) { return "DIRECT";" I tried to ping 127.0.0.1 and I got reply. But I added System.getProperties().put("http.proxyHost", "127.0.0.1"); in my program and it didn't solve the problem
127.0.0.1 and localhost are loopback addresses i.e. they point back to your own machine. So, setting them as proxy host won't help. Do you see any other host or ip address configured in the script?
Okay, I found the following in my PAC file "function FindProxyForURL(url, host) { if(!((url.substring(0,5) == "http:") || (url.substring(0,4) == "ftp:") || (url.substring(0,6) == "https:"))) { return "DIRECT";" which I think it means that http requests are not going through proxy. Is that correct?
You missed the not ! operator. if(!( means if the protocol being used is NOT http, ftp or https then don't use the proxy. What's in the else?
1

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.

2 Comments

I got DNS request timed out when I tried nslookup. I used auto obtain in the wifi's TCP/IP setting. Not sure what DNS I should use if I need to change that.
@SeanYang15 see my comment to your question. Basically, you can't ping and have to go through a proxy.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.