aspose file tools*
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 Spring in Action this week in the Spring 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: 42276
    
  64
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: 14268
    
  21

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 8 API documentation
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
Thanks a lot, Jesper.
Sri Palem
Ranch Hand

Joined: Sep 12, 2009
Posts: 32
I am not able to execute below program. Please help.
I have set my proxies as class variables which i did not specify here.
When i used this URL in browser it is working perfectly fine.
https://chart.googleapis.com/chart?cht=lc&chtt=This%20is%20%7C%20my%20graph&chs=300x300&chxt=y&chd=t%3A40%2C20%2C50%2C20%2C200





When executed this code. It is throwing ConnectionRefused Exception..


java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.perse.swing.browser.SSlPost.getPOSTUrl(SSlPost.java:108)
at com.perse.swing.browser.SSlPost.main(SSlPost.java:35)


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
What does "not able to execute" mean? What, exactly, are you trying to do, and what, exactly, is the result? TellTheDetails
Sri Palem
Ranch Hand

Joined: Sep 12, 2009
Posts: 32
I edited my previous post and pasted exception there. Please have a look.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Does the proxy handle HTTPS? Have you tried with HTTP instead of HTTPS?

Also, use System.setProperty("...", "...") instead of your approach.
Sri Palem
Ranch Hand

Joined: Sep 12, 2009
Posts: 32
Thank you so much . I used http instead of https and it worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpURLConnection