Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. view page source (of a webpage)

view page source (of a webpage)

Scheduled Pinned Locked Moved Java
helpphphtmlapachecom
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharkbc
    wrote on last edited by
    #1

    Hi all ! I want to receive page source from a hyperlink. This is may code

    public class SourceViewer{
    public static void main (String[] args) throws IOException{

    System.out.print("Enter url of local for viewing html source code: ");
    
    
    //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    

    // String url = br.readLine();
    //http://vdict.com/Hello,1,0,0.html
    String url = "http://vdict.com/Hello,1,0,0.html";
    try{
    URL u = new URL(url);
    HttpURLConnection uc = (HttpURLConnection) u.openConnection();
    int code = uc.getResponseCode();
    String response = uc.getResponseMessage();
    System.out.println("HTTP/1.x " + code + " " + response);
    for(int j = 1; ; j++){
    String header = uc.getHeaderField(j);
    String key = uc.getHeaderFieldKey(j);
    if(header == null || key == null)
    break;
    System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
    }
    InputStream in = new BufferedInputStream(uc.getInputStream());
    Reader r = new InputStreamReader(in);
    int c;
    while((c = r.read()) != -1){
    System.out.print((char)c);
    }
    }
    catch(MalformedURLException ex){
    System.err.println(url + " is not a valid URL.");
    }
    catch(IOException ie){
    System.out.println("Input/Output Error: " + ie.getMessage());
    }
    }
    }

    it work well with some link. But problem with this link: http://vdict.com/Hello,1,0,0.html[^] this is the result form browser and my program: -Browser:you can see by your self. -My program:

    Date: Fri, 23 Oct 2009 03:53:22 GMT
    Server: Apache/2.2.3 (Red Hat)
    X-Powered-By: PHP/5.1.6
    Expires: Fri, 30 Oct 2009 03:53:22 GMT
    Cache-Control: max-age=360000, must-revalidate
    Pragma: public
    Last-Modified: Fri, 23 Oct 2009 03:53:22 GMT
    Vary: Accept-Encoding
    Content-Length: 2102
    Content-Type: text/html; charset=UTF-8
    Set-Cookie: PHPSESSID=4hgviiktc0gc38aiotd8s6jn37; path=/
    Connection: Close
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
    <meta name='description' content='Vie

    4 1 Reply Last reply
    0
    • S sharkbc

      Hi all ! I want to receive page source from a hyperlink. This is may code

      public class SourceViewer{
      public static void main (String[] args) throws IOException{

      System.out.print("Enter url of local for viewing html source code: ");
      
      
      //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      

      // String url = br.readLine();
      //http://vdict.com/Hello,1,0,0.html
      String url = "http://vdict.com/Hello,1,0,0.html";
      try{
      URL u = new URL(url);
      HttpURLConnection uc = (HttpURLConnection) u.openConnection();
      int code = uc.getResponseCode();
      String response = uc.getResponseMessage();
      System.out.println("HTTP/1.x " + code + " " + response);
      for(int j = 1; ; j++){
      String header = uc.getHeaderField(j);
      String key = uc.getHeaderFieldKey(j);
      if(header == null || key == null)
      break;
      System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
      }
      InputStream in = new BufferedInputStream(uc.getInputStream());
      Reader r = new InputStreamReader(in);
      int c;
      while((c = r.read()) != -1){
      System.out.print((char)c);
      }
      }
      catch(MalformedURLException ex){
      System.err.println(url + " is not a valid URL.");
      }
      catch(IOException ie){
      System.out.println("Input/Output Error: " + ie.getMessage());
      }
      }
      }

      it work well with some link. But problem with this link: http://vdict.com/Hello,1,0,0.html[^] this is the result form browser and my program: -Browser:you can see by your self. -My program:

      Date: Fri, 23 Oct 2009 03:53:22 GMT
      Server: Apache/2.2.3 (Red Hat)
      X-Powered-By: PHP/5.1.6
      Expires: Fri, 30 Oct 2009 03:53:22 GMT
      Cache-Control: max-age=360000, must-revalidate
      Pragma: public
      Last-Modified: Fri, 23 Oct 2009 03:53:22 GMT
      Vary: Accept-Encoding
      Content-Length: 2102
      Content-Type: text/html; charset=UTF-8
      Set-Cookie: PHPSESSID=4hgviiktc0gc38aiotd8s6jn37; path=/
      Connection: Close
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
      <meta name='description' content='Vie

      4 Offline
      4 Offline
      4277480
      wrote on last edited by
      #2

      The problem is that it does not get two things. 1. The CSS tags in the HTML file 2. The CSS file I am not sure how to get the tags.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups