0% found this document useful (0 votes)
10 views

11 Implementation of HTTP

This document describes the implementation of an HTTP server and client in Java. The server code creates a server socket listening on port 12001, accepts client connections, reads the request, and sends a simple response. The client code opens an HTTP connection to the server, sends a POST request with sample XML data, and prints the response. Running the code outputs the server starting, request details, and response received by the client.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

11 Implementation of HTTP

This document describes the implementation of an HTTP server and client in Java. The server code creates a server socket listening on port 12001, accepts client connections, reads the request, and sends a simple response. The client code opens an HTTP connection to the server, sends a POST request with sample XML data, and prints the response. Running the code outputs the server starting, request details, and response received by the client.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

11 Implementation of HTTP

SERVER
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12001);
System.out.println("HTTP Server (only POST implemented) is ready and is
listening on Port Number 12001 \n");
while(true) {
Socket clientSocket = serverSocket.accept();
System.out.println(clientSocket.getInetAddress().toString() + " " +
clientSocket.getPort());
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
OutputStream out = clientSocket.getOutputStream();
String temp;
while((temp=in.readLine()) != null)
System.out.println(temp);
String response = "HTTP/1.1 200 OK\n\r";
response = response + "Date: Fri, 04 May 2001 20:08:11 GMT\n\r";
response = response + "Server: Sanjits Server\n\r";
response = response + "Connection: close\n\r";

response = response + "1";


byte[] bytes = response.getBytes();
out.write(bytes);
out.flush();
in.close();
out.close();
}
} catch(Exception e) {
System.out.println("ERROR: " + e.getMessage());
System.exit(1);
}}}
CLIENT
public class Client {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:12001");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
String test = "<name>Hello</name>";
byte[] bytes = test.getBytes();
con.setRequestProperty("Content-length", String.valueOf(bytes.length));
con.setRequestProperty("Content-type", "text/html");
OutputStream out = con.getOutputStream();
out.write(bytes);
out.flush();
BufferedReader in = new BufferedReader(new
InputStreamReader(con.getInputStream()));
String temp;
while((temp = in.readLine()) != null)
System.out.println(temp);
out.close();
in.close();
con.disconnect();

} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}}}

SAMPLE OUT-PUT: -
HTTP Server (only POST implemented) is ready and is HTTP Server (only POST
implemented) is ready and is listening on Port Number 12001
127.0.0.1/127.0.0.1 1592
POST / HTTP/1.1
Content-length: 18
Content-type: text/html
User-Agent: Java1.3.1_04
Host: localhost:12001

You might also like