aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes HttpURLConnection - getting the response content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "HttpURLConnection - getting the response content" Watch "HttpURLConnection - getting the response content" New topic
Author

HttpURLConnection - getting the response content

Steve Watson
Ranch Hand

Joined: Apr 08, 2003
Posts: 38
I am trying to call a URL with HttpURLConnection and retrieve the HTML that comes back as a String. eg.:



then what do I do?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8893
    
    8

getContent() or getInputStream(). You don't have to call connect() explicity, as it is called when a method which requires a connection (i.e. the aformentioned methods) is invoked.
You could cut out the middleman and just use url.getInputStream() if you aren't using any HTTP specific features (i.e. POST).


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Steve Watson
Ranch Hand

Joined: Apr 08, 2003
Posts: 38
Thanks your reply. getContent() returns an Object and getInputStream() returns an InputStream - how do I get a string of the HTML out of those?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8893
    
    8

The Java Tutorial has a section on Input Streams. You should take a gander. Read from the InputStream, save the result to some intermediary like a StringBuffer or a StringWriter. When you are done reading, the intermediary will contain all the HTML. When you are comfortable with that, take a look at improving efficiency by using a BufferedReader.
Steve Watson
Ranch Hand

Joined: Apr 08, 2003
Posts: 38
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpURLConnection - getting the response content