Posted By

jerryvig on 09/10/11


Tagged


Versions (?)

Java: Make Simple HTTP Request and Put Response in String


 / Published in: Java
 

This snippet shows how to do a simple lightweight HTTP request in Java and place the response content in a String.

  1. try {
  2. URL myUrl = new URL( "http://www.mydomain.com" );
  3. String responseContent = "";
  4. BufferedReader myReader = new BufferedReader( new InputStreamReader(myUrl.openStream()) );
  5. try {
  6. String line = "";
  7. while ( (line = myReader.readLine()) != null ) {
  8. responseContent += line;
  9. }
  10. myReader.close();
  11. }
  12. catch ( IOException myIOE ) { myIOE.printStackTrace(); }

Report this snippet  

You need to login to post a comment.