URL is acronym for Uniform Resource Locator. URL is the resource address on the internet. Java provides java.net package to interact with URLs.
Steps to follow while calling url.
1- Create URL object.
2- Open connection with url and get stream.
3- Hold stream.
4- Read returned stream.
URL class provides many methods to parse url like getProtocol(), getHost(), getPath(), getAuthority() and getFile() etc.
Following code snippets display two methods to read resources from URLs
First Method: This method uses openStream() method of URL class to get inputStream without opening connection.
package javaWrapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class URLCalling {
public static void main(String[] args) {
String urlStr="http://rsjavawrapper.blogspot.in/";
URL url=null;
BufferedReader br=null;
InputStream is=null;
InputStreamReader isr=null;
try {
//Creating URL object using StringURL.
url = new URL(urlStr);
//Opening url stream and holding Stream, it returns InputStream object.
is=url.openStream();
//Creating InputStreamReader object using returned InputStream to read content.
isr=new InputStreamReader(is);
//Creating BufferedReader object using InputStreamReader.
br = new BufferedReader(isr);
//Reading returned content from url.
String content;
while ((content= br.readLine()) != null)
System.out.println(content);
//Closing BufferedReader.
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Second Method: This method uses openConnection() method of URL class to open connection which returns URLConnection class object and this object is further used to get InputStream object. URLConnection class provides many method that make working ease.
package javaWrapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLCalling {
public static void main(String[] args) {
String urlStr="http://rsjavawrapper.blogspot.in/";
URL url=null;
BufferedReader br=null;
InputStream is=null;
InputStreamReader isr=null;
URLConnection urlCon=null;
try {
//Creating URL object using StringURL.
url = new URL(urlStr);
//Opening connection with url.
urlCon = url.openConnection();
//Getting InputStream object and holding stream.
is=urlCon.getInputStream();
//Creating InputStreamReader object using returned InputStream.
isr=new InputStreamReader(is);
//Creating BufferedReader object using InputStreamReader.
br = new BufferedReader(isr);
//Reading returned content from url.
String content;
while ((content= br.readLine()) != null)
System.out.println(content);
//Closing BufferedReader.
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
No comments:
Post a Comment