This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes HttpURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HttpURLConnection" Watch "HttpURLConnection" New topic
Author

HttpURLConnection

Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
Hi,

I want to read content of the web site through java program. I am using HttpURLConnection class. But I am unable to view the contents. I got code from net "Html source viewer". But what if website is in php, jsp etc.
Any pointers ??

Thanks,
Anand


~ Anand,
SCJP 1.5
SCWCD 1.5
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Reading the content of a web page could be as simple as:


It doesn't matter whether the web site is written in JSP, PHP or Fortran, because either way you're going to get HTML delivered back to you.
[ March 22, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
Hi Ulf,

Solution given by you is not working. See the stack trace below :
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
at sun.net.www.http.HttpClient.New(HttpClient.java:287)
at sun.net.www.http.HttpClient.New(HttpClient.java:299)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:792)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:744)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:669)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:913)
at java.net.URLConnection.getContent(URLConnection.java:682)
at java.net.URL.getContent(URL.java:1021)
at Tp.main(Tp.java:20)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Are you behind a proxy?

Connect through a Proxy
Java Tip 42: Write Java apps that work with proxy-based firewalls

The official documentation: Networking Properties


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
Thanks a lot, Jesper.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HttpURLConnection
 
Similar Threads
HttpURLConnection - getting the response content
How to get a String from an InputStream
Polling from java Web start to web server
EOF error
java.io.IOException: Premature EOF