Commit 4aeae7c2 authored by kiana's avatar kiana

saving and response to requests again is improved

parent f243ea99
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
import java.util.stream.Stream;
public class Fire {
GetResponse savedRequests;
File[] files;
public Fire() {
}
public void fire(int i) {
i--;
if (files[i].getName().contains(".txt")) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(files[i]));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String content = null;
try {
content = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(content);
savedRequests = new GetResponse(content);
}
}
public void showRequest(){
int count = 0;
for (File file : files) {
if (file.getName().contains(".txt") && file.getName().contains(".txt")) {
count++;
System.out.println();
System.out.print(count+" . ");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String content = null;
try {
content = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(content);
String[] contentCopy = content.trim().split(" ");
for (int i = 0; i < contentCopy.length; i++) {
if (contentCopy[i].contains("http://")) {
System.out.print("url: " + contentCopy[i] + "| ");
}
if (contentCopy[i].equals("-M") || contentCopy[i].equals("--method")) {
System.out.print("method: " + contentCopy[i + 1] + "| ");
} else if (contentCopy[i].equals("-H") || contentCopy[i].equals("--headers")) {//setting headers
String[] keyValue = contentCopy[i + 1].trim().split(";");//splitting headers from each other
System.out.print("headers: ");
for (int j = 0; j < keyValue.length; j++) {
String[] keyValueCopy = keyValue[j].trim().split(":");//splitting key and value
String key = keyValueCopy[0];
String value = keyValueCopy[1];
System.out.print(key + ": " + value + "/");
}
i++;
} else if (contentCopy[i].equals("-i")) {
System.out.println("include headers: true" + "| ");
} else if (contentCopy[i].equals("-O") || contentCopy[i].equals("--output")) {
System.out.println("output: true" + "| ");
} else if (contentCopy[i].equals("-d") || contentCopy[i].equals("--data")) {
String cont = contentCopy[i + 1];
System.out.print("messageBody: " + cont + "| ");
}
}
}
}
System.out.println();
}
}
\ No newline at end of file
This diff is collapsed.
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
import java.util.Scanner;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args){
// GetResponse getResponse = new GetResponse();
// getResponse.splitRequest();
// Response response = new Response();
public static void main(String[] args) throws IOException {
GetResponse response = new GetResponse();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Response {
// Extend HttpServlet class
public class Response extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
private static final String USER_AGENT = "Mozilla/5.0";
// Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 5);
private static final String GET_URL = "https://localhost:9090/SpringMVCExample";
// Set response content type
response.setContentType("text/html");
private static final String POST_URL = "https://localhost:9090/SpringMVCExample/home";
// Get current time
Calendar calendar = new GregorianCalendar();
String am_pm;
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
private static final String POST_PARAMS = "userName=Pankaj";
if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
public Response (){
String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
try {
sendGET();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("GET DONE");
try {
sendPOST();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("POST DONE");
}
private static void sendGET() throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
PrintWriter out = response.getWriter();
String title = "Auto Refresh Header Setting";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n"+
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<p>Current Time is: " + CT + "</p>\n"
);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("GET request not worked");
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private static void sendPOST() throws IOException {
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("POST request not worked");
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment