Com Report 1
Com Report 1
Student Details
Name ID
[For Teachers use only: Don’t Write Anything inside this box]
Lab Report Status
Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
lOMoARcPSD|382 166 66
HTTP (Hypertext Transfer Protocol) is a fundamental protocol used for communication on the World Wide
Web. HTTP encompasses several methods, including GET and POST, which are employed to request and
transmit data between clients (typically web browsers) and web servers. This lab focuses on implementing
these methods in a Java application to illustrate their functionality.
2. OBJECTIVES/AIM
HTTP GET and POST are two ways your browser (or any app) talks to a website's server.
• GET is like asking for information. When you open a webpage, search on Google, or view an
image, your browser sends a GET request to the server, and the server sends back the data.
• POST is like sending information. When you fill out a form (like logging in or signing up),
uploading a file, or submitting a comment, your browser sends a POST request to the server with
your data so it can be processed or stored.
4. IMPLEMENTATION
● HTTP(POST) Method
package cse312;
import java.io.BufferedReader;
import java.io.IOException;
import
java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
public class HTTP_GET { public static void main(String[] args) throws
MalformedURLException, IOException {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
String str = "Hi!!! Connection Successfully ";
out.write(str.getBytes());
int responseCode = conn.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_CREATED){
System.out.println( responseCode);
System.out.println(conn.getResponseMessage());
}else{
System.out.println("Go to Home");
}
InputStreamReader in = new InputStreamReader(conn.getInputStream());
BufferedReader buffer = new BufferedReader(in);
String eachline = null;
StringBuffer strBuffer = new StringBuffer();
while((eachline = buffer.readLine())!=null){
strBuffer.append(eachline);
}
System.out.println(strBuffer);
}
}
lOMoARcPSD|382 166 66
● HTTP(POST) Method
package cse312; import
java.io.BufferedReader; import
java.io.IOException; import
java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
public class HTTP_POST {
public static void main(String[] args) throws MalformedURLException,
IOException {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
String str = "Hi!!! Connection Successfully ";
out.write(str.getBytes());
int responseCode = conn.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_CREATED){
System.out.println( responseCode);
System.out.println(conn.getResponseMessage());
}else{
System.out.println("Go to Home");
}
InputStreamReader in = new InputStreamReader(conn.getInputStream());
BufferedReader buffer = new BufferedReader(in);
String eachline = null;
StringBuffer strBuffer = new StringBuffer();
while((eachline = buffer.readLine())!=null){
strBuffer.append(eachline);
}
System.out.println(strBuffer);
}
}
Fig-1.1(POST Method)
lOMoARcPSD|382 166 66
Fig-1.2(GET Method)
The program worked as expected. We sent a GET request to webcode.me and successfully received a
response.
We tested the GET method in different environments, which helped us understand how responses can
vary. However, POST is usually preferred for secure data submission.
7. SUMMARY:
The lab report on the "Implementation of HTTP GET and POST Methods in Java" describes a practical
exploration of using these HTTP methods within a Java application. HTTP GET and POST methods are
fundamental for web communication, enabling clients to request data from servers (GET) or send data to
servers (POST).