Showing posts with label Java Networking. Show all posts
Showing posts with label Java Networking. Show all posts

Monday, August 8, 2011

Get Data from a URL

This program will read data from a Web URL and write to Console. This program can be used to make a Custom Web browser.


import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

public class GetDataFromURL {

public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");

InputStream inStream = yahoo.openStream();
Scanner in = new Scanner(inStream);

while (in.hasNext()) {
System.out.println(in.nextLine());
}
inStream.close();
}
}

Sunday, August 7, 2011

Determine the IP address of a website

You can use Java to determine the IP address of any website.

import java.net.InetAddress;
import java.net.UnknownHostException;

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

// "google.com", "yahoo.com", "hotmail.com" etc.
String hostName = "google.com";
try {
InetAddress in = InetAddress.getByName(hostName);
System.out.println("The IP Address of site is:"
+ in.getHostAddress());
} catch (UnknownHostException e) {
System.out.println(" Invalid Host name or server is down");
}
}
}

Output
The IP Address of site is:74.125.236.81