1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using Java's Net::URL Class To Access URLs

Discussion in 'Java' started by pradeep, Sep 25, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Java's networking classes enable you to implement applications that communicate across a network/remote connection, but the platform also extends into the realm of the Internet and URLs. Java's URL class makes accessing Web resources as easy as accessing a local file. Let's take a look at how you can tap into the power of the URL class and read and write data over URL connections.

    Working With URLs



    A URL identifies resources such as files, Web pages, and Web applications that exist on the Web. It consists of a number of syntactic elements. For example, note the following URL:

    http://www.go4expert.com:1234/mywebapps/JavaApp

    The protocol element is identified as http. The host name is www.go4expert.com. The port number is 1234. The rest of the URL, /mywebapps/JavaApp, identifies the resource to be accessed on the site. The resource, in this case, happens to be a Web application. URLs can also include other elements, such as fragments and query strings.

    Data that is retrieved from a URL can be diverse, which necessitates a uniform mechanism for reading from and writing to URLs. Java offers such a mechanism in its java.net package. The specific class from this package that we want to discuss is the URL class.

    The URL class is an abstraction of the URL identifier. It allows a Java programmer to open a connection to a specific URL, read data from it, write data to it, read and write header information, and perform other operations on the URL. We will discuss how the URL class and the stream classes provided by the java.io package allow you to operate on a URL in much the same manner that you operate on files and socket connections.

    Constructors



    When creating an instance of the java.net.URL class, you can take advantage of a number of public constructors to gain flexibility. For example, the class offers a constructor that takes a complete URL string, a constructor that takes a URL string that is broken up into protocol, host, and file/resource, and a constructor that takes a URL string that is broken up into protocol, host, port, and file. Let's construct an instance of the URL class using a complete URL:

    Code:
        URL aURL = new URL("http://www.go4expert.com:1234/mywebapps/JavaApp");
        
    In this example, an instance of the URL class is created with a complete URL that designates the protocol as http, the host as www.go4expert.com, the port as 1234, and the file/resource as mywebapps/JavaApp. Each of the constructors of the URL class throw a MalformedURLException in the event that the arguments passed in form a URL that is syntactically incorrect.

    Opening An URL Connection



    Once you have successfully created an instance of the URL class, you can begin to call operations on it. But before you can access the resource or content represented by the URL, you must open a connection to it. You can do this with the openConnection method call.

    The openConnection method takes no parameters and, on success, returns an instance of the URLConnection class. Snippet A demonstrates the process of opening a connection to a URL. Once you have a successful connection, you can begin reading and writing to the input and output streams of the URLConnection instance.

    Snippet A
    Code:
        import java.net.*;
        import java.io.*;
        
        public void openURLConnection()
        {
            try
            {
                URL url = new URL("http://www.go4expert.com");
                URLConnection connection = url.openConnection();
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
            }
        }
        

    Reading From An URL



    Using the java.io stream classes to read from a URL is a simple process. Once you have a successful connection, you can retrieve the input stream for the connection and begin reading. URLs can represent resources consisting of a wide variety of data formats. Fortunately, the java.io classes can operate on data returned from URLConnection streams in the same fashion that they operate on file streams or socket streams. Snippet B shows how to read text data from a URL.

    Snippet B
    Code:
        import java.net.*;
        import java.io.*;
        
        public void readFromURL()
        {
            try
            {
                URL url = new URL("http://www.go4expert.com");
                URLConnection connection = url.openConnection();
                connection.setDoInput(true);
                InputStream inStream = connection.getInputStream();
                BufferedReader input =
                new BufferedReader(new InputStreamReader(inStream));
        
                String line = "";
                while ((line = input.readLine()) != null)
                System.out.println(line);
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
            }
        }
        

    Writing To An URL



    It's also simple to write to a URL using the java.io stream classes. Once you have a successful connection, you can retrieve the output stream for the connection and begin writing. Of course, it only makes sense to write to a connection that is expecting data from a client. Also, before retrieving and writing to a URLConnection stream, you need to designate the connection as being write-enabled by setting the Output property to true using the setDoOutput(boolean) method. The java.io classes allow you to write data to URLConnection streams just as you write to file streams or socket streams. Snippet C demonstrates how to write object data to a URL.

    Snippet C
    Code:
        import java.net.*;
        import java.io.*;
        
        public void writeToURL()
        {
            try
            {
                URL url = new URL("http://www.go4expert.com");
                URLConnection connection = url.openConnection();
                connection.setDoOutput(true);
                OutputStream outStream = connection.getOutputStream();
                ObjectOutputStream objectStream = new ObjectOutputStream(outStream);
                objectStream.writeInt(54367);
                objectStream.writeObject("Hello there");
                objectStream.writeObject(new Date());
                objectStream.flush();
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
            }
        }
        

    Other Operations



    You can retrieve other types of information from URL and URLConnection objects, such as the host, port, content length, content encoding, and content type. Using these methods along with the stream I/O classes enables you to build sophisticated Web client applications and services.

    Easy Access To The Web



    The URL class provided by the Java platform enables you to access Web resources with the same power and ease you enjoy when accessing a local file. You don't have to worry about the details of Web communication and can concentrate instead on building useful applications and services around Web resources.
     
    Last edited: Sep 26, 2007
    shabbir likes this.
  2. surfer

    surfer New Member

    Joined:
    Oct 3, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    But how I know what the server-application expects when I send input. For example: I'd like to have google search for "George W. Bush" as a background application using the URL-class as you desribed it. How has the exact datastream to look like. I examined the html-source code of the google-page but I couldn't succeed in writing the correct search-output-stream to my URL-connection.
    A possible way to find out exactly what google expects is to capture the datastream when using this site in the (firefox)browser. But I don't know how to get access to this datastream. Does anybody know how to get access to this data?
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    send the query using the URL Query String!
     
  4. surfer

    surfer New Member

    Joined:
    Oct 3, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Do not understand! Where should I use the URL Query String?
    - in my java-program, but there is no 'setQuery' in the URL-class, only a getQuery but this returns null
    - somewhere in my online-browser - where? - and firefox gives me the entire datastream that is transferred to the url?
     
  5. verci

    verci New Member

    Joined:
    Oct 20, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    waoh its great to see this
     
  6. hem1234

    hem1234 New Member

    Joined:
    Nov 20, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thanks friend. It is very useful for me too.
     
  7. su_java

    su_java New Member

    Joined:
    Apr 22, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Can u plz provide more explanation in using the Query String? :confused:
     
  8. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    For a QueryString, just add the query parameter, while constructing the URL object!
     
  9. su_java

    su_java New Member

    Joined:
    Apr 22, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    but how can we get the variable that stores the query?for eg:if the search form in a website is passing parameters using POST method.Then we must know the name attribute of the textbox in the form.But how can we get that?
     
  10. sukumarp

    sukumarp New Member

    Joined:
    May 4, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    How can i add http header?
    I see that there is getheaderfields function in URL class, however corresponding set operation is missing.

    please let me know

    Thanks,
    Sukumar.
     
  11. michaelsm

    michaelsm New Member

    Joined:
    Aug 28, 2009
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    0
    Thanks for the very nice post and also want too more about this topic.
     
  12. tavy88

    tavy88 New Member

    Joined:
    Sep 10, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Can you please show a sample code of HTTPS posting? Because I know how to post on a HTTP, but when it gets secured it doesn't work anymore. Moreover, I tried in the program to set all certificates as trusted, but still won't work. I know how to make a GET on HTTPS, but don't know how to post on HTTPS.
     
  13. fashionbop

    fashionbop New Member

    Joined:
    Sep 11, 2009
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Shopping from China
    Location:
    Guangzhou, China
    Home Page:
    It's very good !!
     
  14. fiverivers

    fiverivers New Member

    Joined:
    Feb 10, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    ITs request please can you tell the detail of Query String?????????????
     

Share This Page