1

I am designing an application that needs to load HTML content from a specific URL on server side by using Java. How can I solve it?

Regards,

informatik01
  • 16,038
  • 10
  • 74
  • 104
Arthur Ronald
  • 33,349
  • 20
  • 110
  • 136

4 Answers4

4

I have used the Apache Commons HttpClient library to do this. Have a look here: http://hc.apache.org/httpclient-3.x/tutorial.html

It is more feature rich than the JDK HTTP client support.

David Tinker
  • 9,383
  • 9
  • 66
  • 98
  • 1
    *Update*. Quote from the Apache Commons HttpClient home page: "The Commons HttpClient project is now end of life, and is no longer being developed. **It has been replaced** by the [Apache HttpComponents](http://hc.apache.org/) project in its [HttpClient](http://hc.apache.org/httpcomponents-client-ga) and [HttpCore](http://hc.apache.org/httpcomponents-core-ga/) modules, which offer better performance and more flexibility." – informatik01 Jan 09 '14 at 16:49
1

If all you need is read the url you do not need to resort to third party libraries, java has built in support to retrieve urls.


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

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
0

If it was php, you could use cURL, but since it's java, you would use HttpURLConnection, as I just found out on this question:

cURL equivalent in JAVA

Community
  • 1
  • 1
Anthony
  • 36,459
  • 25
  • 97
  • 163
0

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection;

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

    URL url;

    try {
        // get URL content

        String a="http://localhost:8080//TestWeb/index.jsp";
        url = new URL(a);
        URLConnection conn = url.openConnection();

        // open the stream and put it into BufferedReader
        BufferedReader br = new BufferedReader(
                           new InputStreamReader(conn.getInputStream()));

        String inputLine;
        while ((inputLine = br.readLine()) != null) {
                System.out.println(inputLine);
        }
        br.close();

        System.out.println("Done");

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

Vaibs
  • 2,018
  • 22
  • 29