Commit f243ea99 authored by kiana's avatar kiana

first commit

parents
Pipeline #4987 failed with stages
import javax.imageio.IIOException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;
import java.nio.file.Files;
import java.io.File;
import java.io.FileWriter;
import java.net.URLEncoder;
import java.util.Map;
public class GetResponse {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "https://localhost:9090/SpringMVCExample";
private static final String POST_URL = "https://localhost:9090/SpringMVCExample/home";
private static final String POST_PARAMS = "userName=Pankaj";
Map<String, String> params = new HashMap<>();
//params.put("v", "dQw4w9WgXcQ");
String requestMethod;
String headers;
Boolean invisible;
String help;
String arg;
String address;
String completeRequest;
String content;
Scanner scanner = new Scanner(System.in);
public GetResponse(){
splitRequest();
try {
response();
} catch (IOException e) {
e.printStackTrace();
}
}
public String[] splitRequest() {
completeRequest = scanner.next();
String[] splittedRequest = completeRequest.split("\\s");
address = splittedRequest[0];
return splittedRequest;
}
public void response() 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);
StringBuilder requestData = new StringBuilder();
for (Map.Entry<String, String> param : params.entrySet()) {
if (requestData.length() != 0) {
requestData.append('&');
}
// Encode the parameter based on the parameter map we've defined
// and append the values from the map to form a single parameter
requestData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
requestData.append('=');
requestData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
// Convert the requestData into bytes
byte[] requestDataByes = requestData.toString().getBytes("UTF-8");
String[] splittedRequestCopy = splitRequest();
for (int i = 0; i < splittedRequestCopy.length - 1; i++) {
if (splittedRequestCopy[i].equals("-M") || splittedRequestCopy[i].equals("--method")) {
if (splittedRequestCopy[i + 1].equals("GET") || splittedRequestCopy[i + 1].equals("POST") || splittedRequestCopy[i + 1].equals("HEAD")
|| splittedRequestCopy[i + 1].equals("DELETE") || splittedRequestCopy[i + 1].equals("PUT") || splittedRequestCopy[i + 1].equals("CONNECT")
|| splittedRequestCopy[i + 1].equals("OPTIONS") || splittedRequestCopy[i + 1].equals("TRACE") || splittedRequestCopy[i + 1].equals("PATCH"))
con.setRequestMethod(splittedRequestCopy[i + 1]);
} else if (splittedRequestCopy[i].equals("-H") || splittedRequestCopy[i].equals("--header")) {
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Content-Length", String.valueOf(requestDataByes.length));
} else if (splittedRequestCopy[i].equals("-i")) {
invisible = true;
} else if (splittedRequestCopy[i].equals("-O") || splittedRequestCopy[i].equals("--output")) {
try {
content = con.getResponseMessage();
} catch (IOException e) {
e.printStackTrace();
}
try {
File newTextFile = new File("C:/thetextfile.txt");
FileWriter fw = new FileWriter(newTextFile);
fw.write(content);
fw.close();
} catch (IOException iox) {
iox.printStackTrace();
}
}
content = con.getResponseMessage();
System.out.println(content);
int code = con.getResponseCode();
System.out.println(code);
}
}
}
import java.io.IOException;
public class Main {
public static void main(String[] args){
// GetResponse getResponse = new GetResponse();
// getResponse.splitRequest();
// Response response = new Response();
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;
public class Response {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "https://localhost:9090/SpringMVCExample";
private static final String POST_URL = "https://localhost:9090/SpringMVCExample/home";
private static final String POST_PARAMS = "userName=Pankaj";
public Response (){
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();
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 {
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");
}
}
}
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