You are on page 1of 3

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Summer 2021), B.Sc. in CSE (Evening)

LAB REPORT NO # 03
Course Title: Computer Networking Lab
Course Code: CSE 312 Section: 192EA

Lab Experiment Name: Implementation of HTTP POST Method in JAVA

Student Details

Name ID
1. Nahid Hasan Ridoy 192015031

Lab Date : Nov 5, 2021


Submission Date : Dec 28, 2021
Course Teacher’s Name : Tanpia Tasnim

[For Teachers use only: Don’t Write Anything inside this box]

Lab Report Status Marks: …………………………………


Comments:.............................................. Signature:.....................
Date:..............................
1. Objective(s)
• To learn about and solve HTTP POST Method using JAVA in this
lab Experiment.

2. IMPLEMENTATION / CONFIGURATION

package lab02;
import java.net.URL;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class lab02 {

public static void main(String[] args) throws IOException {


final String POST = "{\n" + " \"Name\": \"D. M. Shanto Islam\",\r\n"
+ " \"S_Id\": \"191015138\",\r\n"
+ " \"id\": \"191E\",\r\n"
+ " \"Result\": \"Successful\"" + "\n}";
System.out.println(POST);
URL url = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection postConnection = (HttpURLConnection) url.openConnection();
postConnection.setRequestMethod("POST");
postConnection.setRequestProperty("S_Id", "a1bcdefgh");
postConnection.setRequestProperty("Content-Type", "application/json");
postConnection.setDoOutput(true);
OutputStream write = postConnection.getOutputStream();
write.write(POST.getBytes());
write.flush();
write.close();
int response = postConnection.getResponseCode();
System.out.println("POST Response : " + response);
System.out.println("POST Response Message : " + postConnection.getResponseMessage());
if (response == HttpURLConnection.HTTP_CREATED) {
BufferedReader in = new BufferedReader(new InputStreamReader(
postConnection.getInputStream()));
String inputLine;
StringBuffer response1 = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response1.append(inputLine);
}
in.close();
System.out.println(response1.toString());
} else {
System.out.println("POST NOT WORKED");
}
}
}
Figure:

Result:
I implement this code successfully.

SUMMARY
I complete this lab experiment successfully.

You might also like