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.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 class Main {
public static void main(String[] args){ public static void main(String[] args) throws IOException {
// GetResponse getResponse = new GetResponse();
// getResponse.splitRequest();
// Response response = new Response();
GetResponse response = new GetResponse(); GetResponse response = new GetResponse();
} }
} }
import java.io.BufferedReader; // Import required java libraries
import java.io.IOException; import java.io.*;
import java.io.InputStreamReader; import javax.servlet.*;
import java.io.OutputStream; import javax.servlet.http.*;
import java.net.HttpURLConnection; import java.util.*;
import java.net.URL;
// Extend HttpServlet class
public class Response { public class Response extends HttpServlet {
// Method to handle GET method request.
private static final String USER_AGENT = "Mozilla/5.0"; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
private static final String GET_URL = "https://localhost:9090/SpringMVCExample";
// Set refresh, autoload time as 5 seconds
private static final String POST_URL = "https://localhost:9090/SpringMVCExample/home"; response.setIntHeader("Refresh", 5);
private static final String POST_PARAMS = "userName=Pankaj"; // Set response content type
response.setContentType("text/html");
public Response (){
// Get current time
try { Calendar calendar = new GregorianCalendar();
sendGET(); String am_pm;
} catch (IOException e) { int hour = calendar.get(Calendar.HOUR);
e.printStackTrace(); int minute = calendar.get(Calendar.MINUTE);
} int second = calendar.get(Calendar.SECOND);
System.out.println("GET DONE");
try { if(calendar.get(Calendar.AM_PM) == 0)
sendPOST(); am_pm = "AM";
} catch (IOException e) { else
e.printStackTrace(); am_pm = "PM";
}
System.out.println("POST DONE"); String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
}
PrintWriter out = response.getWriter();
private static void sendGET() throws IOException { String title = "Auto Refresh Header Setting";
URL obj = new URL(GET_URL); String docType =
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT); out.println(docType +
int responseCode = con.getResponseCode(); "<html>\n" +
System.out.println("GET Response Code :: " + responseCode); "<head><title>" + title + "</title></head>\n"+
if (responseCode == HttpURLConnection.HTTP_OK) { // success "<body bgcolor = \"#f0f0f0\">\n" +
BufferedReader in = new BufferedReader(new InputStreamReader( "<h1 align = \"center\">" + title + "</h1>\n" +
con.getInputStream())); "<p>Current Time is: " + CT + "</p>\n"
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("GET request not worked");
}
} }
private static void sendPOST() throws IOException { // Method to handle POST method request.
URL obj = new URL(POST_URL); public void doPost(HttpServletRequest request, HttpServletResponse response)
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); throws ServletException, IOException {
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(); doGet(request, response);
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