Now, I'm going to explain a program which will send a gentle HTTP request to the web server and displays the response headers and response body. This program will shows how a web browser fetches the information.


import java.io.*;
import java.net.*;

/**
* Shows how to connect to a web server using correct protocol
* @author Santhosh Reddy Mandadi
* @since 15-March-2007
* @version 1.4
*/
class WebClient
{
static public void main(String[] args)throws Exception
{
//create a Socket
try
{
//String a=args[0];
byte ipaddr[]={(byte)208,(byte)198,(byte)128,(byte)208};
Socket s = new Socket(InetAddress.getByAddress(ipaddr),80);
//Socket s=new Socket(InetAddress.getLocalHost(),8990);
System.out.println("Connected to Server Successfully");
PrintStream ps =new PrintStream(s.getOutputStream());
InputStream is = s.getInputStream();
//ps.println("GET / HTTP/1.1 \r\nHOST:inetsolv\r\n\r");
//ps.println("GET /test1/login.jsp");
ps.println("GET /cmint/home.html HTTP/1.1\r\nHOST:inetsolv\r\n\r\n");
byte b[] = new byte [10000];
// this is definitely a bad way of getting a web document
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
String str="";
while(str!=null)
{
System.out.println(str);
str=reader.readLine();
}
s.close();
}catch(Exception e){ System.out.println(e);}
}
}


There are several ways to find the server and open the socket through as you can see comments from the above program.

Click here to learn more on HTTP.

Click here to check out other programs that I've developed.