Results 1 to 10 of 10
  1. #1
    asheeshiit is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Smile read html code of web page

    Can anyon tell me how to read html code of a web page using java:)

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    8

    Default use URL and URLConnection classes

    // a sample code

    import java.net.*;
    import java.io.*;
    class ReadHtml{

    public static void main(String[] arg){
    try{
    URL url=new URL("http://www.yahoo.com");
    System.out.println("Connecting to www.yahoo.com");
    URLConnection ucon=url.openConnection();
    System.out.println("Connectied to www.yahoo.com");
    System.out.println("Retrieving contents from www.yahoo.com");
    String htmlContents=getResponseData(ucon);
    System.out.println("Retrieved contents from Yahoo! as follows");
    System.out.println(htmlContents);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

    private static String getResponseData(URLConnection conn) throws Exception{
    StringBuffer sb=new StringBuffer();
    String data="";
    InputStream is=conn.getInputStream();
    int ch;
    while((ch=is.read()) != -1){
    sb.append((char)ch);
    }

    data=sb.toString();
    is.close();
    is=null;
    sb=null;
    System.gc();
    return data;
    }

    }

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    9

    Default

    See my introduction to networking in Java for an example. I'd recommend:
    - buffering the input
    - being careful about character conversion
    - making sure you close the socket in a finally clause
    By the way, don't put spurious <i>System.gc()</i> calls in your code. It's usually a bad idea and I certainly can't see any point here.

  4. #4
    snehal is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default wow thx neil

    I have been searching the web for an example like this but couldnt find one so simple and good a beginner to start learning.

    You just gave me courage to start learning it again. Do you have a site where i can learn more on this thing.

    If i had to specify a username and password to log into a website for example webmail or cooperate website how is it done?

    thank you neil.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,433
    Rep Power
    29

    Default

    to specify a username and password to log into a website
    This gets a bit more complicated for a beginner. If the protocol being used by the website is HTTPS then its very difficult. If HTTP, then its possible.
    You'll need to understand the HTTP protocol. That describes what a client sends to a server and what the server returns.
    Read up on that, do some thinking and come back with your questions.

  6. #6
    snehal is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    To Norm.
    I know html and PHP and the page is http. I agree https would too difficult for a beginner. What do you advise me to read and if you know a good link?

    Neil
    If you can just attach a code for example to read this page by logging in using my username and password. It would be of great help for my education.

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    204
    Rep Power
    0

    Default Re: read html code of web page

    Only in Russia.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,373
    Blog Entries
    7
    Rep Power
    25

    Default Re: read html code of web page

    Quote Originally Posted by zFollette View Post
    Only in Russia.
    Only in Russia what? Please quote the part(s) of the original reply you're answering; there are more people reading this forum than you and the other poster.

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    204
    Rep Power
    0

    Default Re: read html code of web page

    Quote Originally Posted by JosAH View Post
    Only in Russia what? Please quote the part(s) of the original reply you're answering; there are more people reading this forum than you and the other poster.

    Jos
    Sorry, I was referring to the (deleted) very obvious russian porn/game post.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,373
    Blog Entries
    7
    Rep Power
    25

    Default Re: read html code of web page

    Quote Originally Posted by zFollette View Post
    Sorry, I was referring to the (deleted) very obvious russian porn/game post.
    Ah, got it; never respond to spam; it'll be deleted asap (the forums are infested by spam now and then) and any repsonses would leave the threads incomprehensible or just incoherent looking; no hard feelings though and no need to apologize ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. How to open html page?
    By smartsubroto in forum New To Java
    Replies: 8
    Last Post: 08-16-2012, 12:13 PM
  2. convert html page to pdf
    By MarkWilson in forum Advanced Java
    Replies: 2
    Last Post: 09-02-2008, 11:14 PM
  3. How to read a Web Page through java program
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-04-2008, 02:37 PM
  4. How to read a Web Page through java program
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:31 PM
  5. HTML page
    By bbq in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •