java.net unable to connect to URL

Posted on 2010-04-05
Medium Priority
Last Modified: 2013-11-23
I am trying to connect to a url using java.net to connect to a url but it keeps giving me an UnknownHostException: www.yahoo.com. I have tried everything. The firewall is not blocking java, i am able to ping the website from the computer running the code. I also tested with adding a line for proxy's to allow for the connection but that did not work either. I tried some other websites hosted on my servers as well but with no luck.
import java.net.*;
import java.io.*;

public class URLconnectionReader {
    public static void main(String[] args) throws Exception {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("", 8080));
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
        String inputLine;

        while ((inputLine = in.readLine()) != null) 

Open in new window

Question by:abhinav90

Author Comment

ID: 29807058
i just tested something. If i change the host to localhost and try and access some http content hosted on my wamp server, then the output was perfect. This is ofcourse after removing the proxy statement on line 6. So i guess the code syntax is fine, there is some other issue.
LVL 20

Accepted Solution

Venabili earned 2000 total points
ID: 29857763
YOu know that you are not using the Proxy even though you declare it, right?

The code looks ok so I suspect you simply need to configure the proxy. So try to use the Proxy and post back the code with the proxy being used and the error that yiou have after this.

Try to start the program as follows:
java -DproxySet=true -DproxyHost=proxyhost URLconnectionReader

Or alternatovely use:
URL url = new URL("http", "", 8080, "http://www.yahoo.com/");
(at least I think that worked..)
See http://www.rgagnon.com/javadetails/java-0085.html for the other options.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Cloud file services can fill many different roles for your business. Often, the use of cloud file services begins with employees using consumer products, like Dropbox, to share files with customers and each other. While sync-and-share can be an effe…
As a financial services provider, your business is impacted by two of the strictest federal regulations on record: the Sarbanes-Oxley Act and the Gramm-Leach-Bliley Act. Correctly implementing faxing into your organization to provide secure, real-ti…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question