retrieving contents of a page


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: retrieving contents of a page

  1. #1
    Join Date
    Jan 2004
    Posts
    2

    retrieving contents of a page

    I need to do something in a java class where I can specify a URL to retrieve the contents of that url (so all the html for it).. I'm sure this is something straight-forward, I just don't know how to do it. Any help is appreciated. Thanks.

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    heres something i threw together just now. its not pretty, but will give you the basic idea:

    Code:
    public class urler{
    
      //usage from command line: java urler <url to dump>
      public static void main(String[] argv)throws Exception{
        
        //make new URL, and call its openConnection() method. That gets us an HttpURLCOnnection object
        //we cant make one directly because it is an abstract class
        java.net.HttpURLConnection h = (java.net.HttpURLConnection)(new java.net.URL(argv[0])).openConnection();
        
        //do a get of the information
        h.setRequestMethod("GET");
        h.connect();
        
        //prints something like HTTP 200 OK or HTTP 400 Not Found
        System.out.println("HTTP "+h.getResponseCode()+" "+h.getResponseMessage());
    
        //how long is this thing anyways?
        System.out.println("Expecting "+h.getContentLength()+" bytes:");
    
        //buffered reader makes reading the response strings a doddle. InputStreamReader converts 
        //characters properly for us
        java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(h.getInputStream()));
    
        //read a line of the input
        String stuff = br.readLine();
        
        //buffered readers return null when tehre is nothing more to be read
        while(stuff!=null){
          System.out.println(stuff);
          stuff = br.readLine();
        }
      }
    }
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center