HOWTO open URL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dave

    HOWTO open URL

    I am trying to open a url and read the data into a string.

    for example,
    I want to see the raw html within www.sourceforge.com put into a String.
    Then I'll parse out the data I need.

    thanks for any help.



  • Phil

    #2
    Re: HOWTO open URL

    Do you want all of it put
    into one string or do you
    want to read it a line at a time?

    Phil..

    "dave" <nospam@nospam. net> wrote in message news:gkR_a.9144 $rh1.172@fe04.a tl2.webusenet.c om...[color=blue]
    > I am trying to open a url and read the data into a string.
    >
    > for example,
    > I want to see the raw html within www.sourceforge.com put into a String.
    > Then I'll parse out the data I need.
    >
    > thanks for any help.
    >
    >
    >[/color]

    Comment

    • dave

      #3
      Re: HOWTO open URL

      All is fine

      "Phil" <rynes@ieee.org > wrote in message
      news:1BR_a.1053 74$cF.31350@rwc rnsc53...
      Do you want all of it put
      into one string or do you
      want to read it a line at a time?

      Phil..

      "dave" <nospam@nospam. net> wrote in message
      news:gkR_a.9144 $rh1.172@fe04.a tl2.webusenet.c om...[color=blue]
      > I am trying to open a url and read the data into a string.
      >
      > for example,
      > I want to see the raw html within www.sourceforge.com put into a String.
      > Then I'll parse out the data I need.
      >
      > thanks for any help.
      >
      >
      >[/color]



      Comment

      • Sandip Chitale

        #4
        Re: HOWTO open URL

        import java.net.*;
        import java.io.*;

        public class URLReader {
        public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.sourceforge .com");
        BufferedReader in = new BufferedReader(
        new InputStreamRead er(
        yahoo.openStrea m()));

        String inputLine;

        while ((inputLine = in.readLine()) != null)
        System.out.prin tln(inputLine);

        in.close();
        }
        }

        from http://java.sun.com/docs/books/tutor...eadingURL.html

        HTH
        sandip



        "dave" <nospam@nospam. net> wrote in message news:<gkR_a.914 4$rh1.172@fe04. atl2.webusenet. com>...[color=blue]
        > I am trying to open a url and read the data into a string.
        >
        > for example,
        > I want to see the raw html within www.sourceforge.com put into a String.
        > Then I'll parse out the data I need.
        >
        > thanks for any help.[/color]

        Comment

        • D Goldman

          #5
          Re: HOWTO open URL

          You can do the following

          URL url = new URL(urlString);
          InputStream in = url.openStream( );
          BufferedReader br = new BufferedReader( new InputStreamRead er(in));
          while ( (aLine = br.readLine()) != null ) {
          //do something with aLine
          }

          Hope this helps,
          Daniel Goldman

          Comment

          • John

            #6
            Re: HOWTO open URL

            Some example working code:
            Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.


            dave wrote:
            [color=blue]
            >I am trying to open a url and read the data into a string.
            >
            >for example,
            >I want to see the raw html within www.sourceforge.com put into a String.
            >Then I'll parse out the data I need.
            >
            >thanks for any help.
            >
            >
            >
            >
            >[/color]

            Comment

            • JavaJunkie

              #7
              Re: HOWTO open URL


              import java.io.*;
              import java.net.*;

              public class UrlVisit {

              /** Creates a new instance of UrlVisit */
              public UrlVisit() {
              }

              public boolean visitWebPage(St ring UrlToVisit) {

              boolean urlVisited = false;
              try {
              URL url = new URL(UrlToVisit) ;
              URLConnection urlConnection = url.openConnect ion();
              BufferedReader htmlPage = new BufferedReader( new
              InputStreamRead er(url.openStre am()));

              String line = "";
              while((line = htmlPage.readLi ne()) != null) {
              //do something with the html line
              System.out.prin tln(line);
              }
              htmlPage.close( );
              urlConnection = null;
              urlVisited = true;
              } catch(Exception e) {
              urlVisited = false;
              }
              return urlVisited;
              }
              /**
              * @param args the command line arguments
              */
              public static void main(String[] args) {
              UrlVisit urlVisit = new UrlVisit();
              String url = null;
              String commstart = " <!-- *************** ******** ";
              String commend = " *************** ******** -->";
              if (args[0] == null) {
              url = "http://www.cnn.com";
              } else {
              url = args[0];
              System.out.prin tln(commstart + "Visiting "+ url + commend);
              }
              if (urlVisit.visit WebPage(url)) {
              System.out.prin tln(commstart + url + " visited" + commend);
              } else {
              System.out.prin tln(commstart + url + " Website not found" +
              commend);
              }
              }
              }


              "John" <noapam@nospam. com> wrote in message
              news:RM2dnVHmAv WcO5Td4p2dnA@ma gma.ca...[color=blue]
              > Some example working code:
              > http://www.javapractices.com/Topic147.cjp
              >
              > dave wrote:
              >[color=green]
              > >I am trying to open a url and read the data into a string.
              > >
              > >for example,
              > >I want to see the raw html within www.sourceforge.com put into a String.
              > >Then I'll parse out the data I need.
              > >
              > >thanks for any help.
              > >
              > >
              > >
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...