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

Friday, January 01, 2010

Check URL Exists or not In Java

Links to this post
In order to check that url exsists or not, you need to do following.
  • Create instance of URL
  • Open connection to url using url.openConnection
  • Create instance of HttpURLConnection
  • Check that httpURLConnection.getResponseCode() is equal to 200 or not.

Friday, August 28, 2009

How to Parse URL in Java

Links to this post
What is URL? URL represents Uniform Resource Locator. It's a pointer to a "resource" on the WorldWide Web.
A resource can be something as simple as a file or a directory,or it can be a reference to a more complicated object,such as a query to a database or to a search engine.

A URL can optionally specify a "port", which is the port number to which the TCP connection is made on the remote host machine. If the port is not specified, the default port for the protocol is used instead. For example, the default port for http is 80.

Java Class java.net.URL provides number of methods such as getFile(),getHost(),getQuery() etc to deal with URL. Please refer to J2SE 6 Documentation for URL Class.

Here I will show you how You can retrieve different parts of URL using Java Program.

Retrieve HTTP Header information using Java

Links to this post
HttpURLConnection java class is used to retrieve HTTP Header Information. HttpURLConnection class provides connect() and disconnect() method in order to connect to url or disconnect.

You can check that whether connection to URL is successfull or not by checking response Code. if response code if 200 and Response message is OK then connection is successfull.

HttpURLConnection provides two useful method called getHeaderField() and getHeaderFieldKey(). Using these methods you can get key and value of HTTP Header and can retrieve HTTP Header information.Please have a look at below java program which retrieves http header information.

Java Program to Retrieve Header Information: 

Retrieve IP information of my machine

Links to this post
Sometimes you require to retrieve IP information of current machine your are running using Java Program.
Using following program you can get  main Local IP Address,main local host name, alt local ip addresses,alt local host names.

Java provides class java.net.InetAddress which presents an Internet Protocol (IP) address.There are some of useful methods as below which one can deal with to retrieve host name from IP and from IP to host name.

Useful InetAddress methods :
1. getLocalHost() -return local host
2. getByName(String host)  - Determines the IP address of a host, given the host's name
3. getAllByName(String host)- Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system

Retrieve list of Local Interfaces on a machine using Java

Links to this post
package com.anuj.utils;

import java.net.*;
import java.util.*;

public class InterfaceLister {
    public static void main(String[] args) throws SocketException {        
        Enumeration enu = NetworkInterface.getNetworkInterfaces();
        while (enu.hasMoreElements()) {
            NetworkInterface net = (NetworkInterface) enu.nextElement();
            System.out.println(net);
        }
    }
}