12/22/09

url example.. loading the website content and calling external api

package testproject;

/**
*
* @author aghuram
*/
import java.net.*;
import java.io.*;
public class URLExample {
void read() throws Exception
{
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();
}
void call()throws Exception
{
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yahooConnection = yahoo.openConnection();
yahooConnection.connect();

}
void writeUrl()throws Exception
{
String dis="hello from raghu";
//pass the url as parameter to the constructor
URL url = new URL("blah blah");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());


out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();



}
public static void main(String[] args) {
try{
URLExample u=new URLExample();
//u.call();
u.writeUrl();


}catch(Exception e){
e.printStackTrace();
}
}
}

No comments:

Post a Comment

Popular Posts