Trouble getting InputStream from URLConnection

I am having a problem getting an InputStream object from a URLConnection object with the getInputStream() method. When my code didn't work I tested it on a few different sites and it works fine on all the sites except the site I want. When I call the getInputStream() method the program just hangs. The site I am trying to connect to is "http://www.digg.com". Is it possible for someone else to try get an InputStream from this site.

Message was edited by:

andyrobo60

[488 byte] By [andyrobo60a] at [2007-11-27 1:04:39]
# 1

I don't want to write a test code from scratch on my own.

Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html

Give a small help to us if you want help from us.

hiwaa at 2007-7-11 23:39:44 > top of Java-index,Java Essentials,Java Programming...
# 2

I used some software that I usually use from behind a proxy server (prompts for name/password etc and does other stuff). It's too long to post, but I found the same thing as the OP - namely that http://www.digg.com/ hangs.

I solved it by adding uc.setRequestProperty("User-Agent", "Firefox");

uc is the URLConnection I am using. It seems the site doesn't like whatever User-Agent is sent by default. (Possibly defense against robots or whatever.)

pbrockway2a at 2007-7-11 23:39:44 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks pbrockway2 that worked.

Sorry I didn't post the code but when my code didn't work I changed it to the simplest code I could think of.

try{

URL url = new URL(http://www.digg.com);

URLConnection con = url.openConnection();

InputStream in = con.getInputStream();

}catch(MalformedURLException ex){}

After this didn't work then I posted on the forums and couldn't see the point in posting something so simple.

andyrobo60a at 2007-7-11 23:39:44 > top of Java-index,Java Essentials,Java Programming...