import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class HttpArgumentParser {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java HttpArgumentParser <url>");
return;
}
String url = args[0];
try {
String response = fetchUrlContent(url);
parseArguments(response);
} catch (IOException e) {
System.err.println("Error fetching URL: " + e.getMessage());
}
}
private static String fetchUrlContent(String url) throws IOException {
URL u = new URL(url);
URLConnection connection = u.openConnection();
connection.setConnectTimeout(5000); // Set a timeout
connection.setReadTimeout(10000); // Set a timeout
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line).append(System.lineSeparator());
}
return response.toString();
}
}
private static void parseArguments(String response) {
//Example parsing - adapt to your expected format.
String[] headers = response.split("\n"); //Split into headers and body
if(headers.length > 0){
String[] headerLine = headers[0].split(": ");
if(headerLine.length == 2){
String headerName = headerLine[0];
String headerValue = headerLine[1];
System.out.println("Header: " + headerName + " = " + headerValue);
}
}
//Example: Look for a specific parameter in the body.
if (response.contains("param1=value1¶m2=value2")) {
String body = response.substring(response.indexOf("param1=value1") + "param1=value1".length());
String[] params = body.split("&");
for (String param : params) {
String[] keyValue = param.split("=");
if(keyValue.length == 2){
System.out.println("Parameter: " + keyValue[0] + " = " + keyValue[1]);
}
}
}
}
}
Add your comment