Hi,
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,
Hi,
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,
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:
http://stackoverflow.com/questions/116650/curl-equivalent-in-java
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.
If you have some extra time, groovy has some nice build in features to do stuff like this.
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();
}
}