• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpURLConnection - getting the response content

 
Steve Watson
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Steve Watson
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic