This discussion is archived
6 Replies Latest reply: Apr 15, 2013 6:24 AM by 1002854 RSS

how to open a url

1002854 Newbie
Currently Being Moderated
I am new to Java. Just finished my java1 course. For my work I need open a url every time I want to refresh system with an update. Each update has a number so my urls are url1, url2, url3, ... I know how to put it in a loop to increment the urls in a String. I do not know how to make it open and close the url in a browser. I appreaciate if somebody could provide complete code. Thank you very much.

public class Test {
          
     public static void main( String args[]) {  

     for (int i = 201; i < 300; i++){
System.out.println("http://url=" + i);

     }
     }
}

Somebody helped me with the following code and instructed me to dowload commons io from http://commons.apache.org/proper/commons-io/download_io.cgi and so I did. I do not know how to put it all together and what should I impor....;
Thank you for your help                     
               try {
                    String searchKeyUpdatePage = IOUtils.toString(new URL( "http://www.google.com" ) ) ;
                    System.out.println( searchKeyUpdatePage ) ;
               } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
  • 1. Re: how to open a url
    1002857 Newbie
    Currently Being Moderated
    import java.net.HttpURLConnection;
    import java.io.*;
    import java.net.*;
    class openurl
    {
    public static void main(String []manav)
    {
    try {
    URL myURL = new URL("http://google.com/");
    URLConnection myURLConnection = myURL.openConnection();
    myURLConnection.connect();
    }
    catch (MalformedURLException e) {
    System.out.println("unable to open url");
    }
    catch (IOException e) {  
    System.out.println("open connection fail");
    }

    }

    }
    i think it will help you.
  • 2. Re: how to open a url
    EJP Guru
    Currently Being Moderated
    I doubt it. It doesn't do anything on the network apart from looking up the hostname. You could at least have posted some code that did some I/O.

    @OP:

    1. Questions about 3rd-party libfraries are off topic here.
    2. What is the point of the main() you posted?
    3. What part of java.net.HttpURLConnection don't you understand?
  • 3. Re: how to open a url
    1002854 Newbie
    Currently Being Moderated
    Thank you for your reply.

    " What part of java.net.HttpURLConnection don't you understand? " - As I stated I am beginer in Java. I think what I need here is very simple piece of code. If you could help and write it I appreciate it.
  • 4. Re: how to open a url
    EJP Guru
    Currently Being Moderated
    See the official Java Networkijg Tutorial.
  • 5. Re: how to open a url
    1002854 Newbie
    Currently Being Moderated
    Thank you for your reply and for the code.
  • 6. Re: how to open a url
    gimbal2 Guru
    Currently Being Moderated
    999851 wrote:
    Thank you for your reply and for the code.
    Great, you got some code thrown at you. Do you now know:

    a) what it does
    b) how to apply it
    c) if no to either a) or b), do you know where to get more information so you can start to understand?

    See to me c) would be far more valuable than getting a snippet of code; code without understanding is still meaningless. Knowing where to go to gain understanding is gold.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points