1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. public class HttpArgumentParser {
  7. public static void main(String[] args) {
  8. if (args.length != 1) {
  9. System.out.println("Usage: java HttpArgumentParser <url>");
  10. return;
  11. }
  12. String url = args[0];
  13. try {
  14. String response = fetchUrlContent(url);
  15. parseArguments(response);
  16. } catch (IOException e) {
  17. System.err.println("Error fetching URL: " + e.getMessage());
  18. }
  19. }
  20. private static String fetchUrlContent(String url) throws IOException {
  21. URL u = new URL(url);
  22. URLConnection connection = u.openConnection();
  23. connection.setConnectTimeout(5000); // Set a timeout
  24. connection.setReadTimeout(10000); // Set a timeout
  25. try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
  26. StringBuilder response = new StringBuilder();
  27. String line;
  28. while ((line = reader.readLine()) != null) {
  29. response.append(line).append(System.lineSeparator());
  30. }
  31. return response.toString();
  32. }
  33. }
  34. private static void parseArguments(String response) {
  35. //Example parsing - adapt to your expected format.
  36. String[] headers = response.split("\n"); //Split into headers and body
  37. if(headers.length > 0){
  38. String[] headerLine = headers[0].split(": ");
  39. if(headerLine.length == 2){
  40. String headerName = headerLine[0];
  41. String headerValue = headerLine[1];
  42. System.out.println("Header: " + headerName + " = " + headerValue);
  43. }
  44. }
  45. //Example: Look for a specific parameter in the body.
  46. if (response.contains("param1=value1&param2=value2")) {
  47. String body = response.substring(response.indexOf("param1=value1") + "param1=value1".length());
  48. String[] params = body.split("&");
  49. for (String param : params) {
  50. String[] keyValue = param.split("=");
  51. if(keyValue.length == 2){
  52. System.out.println("Parameter: " + keyValue[0] + " = " + keyValue[1]);
  53. }
  54. }
  55. }
  56. }
  57. }

Add your comment