Get Credential Information From the URL(GET Method) in Java

Given a URL as a string, the task is to extract username, password, profile, role, and key from the URL in a GET Method.

Examples:

Input: 
http://www.geeksforgeeks.com/signin/service?username=test&pwd=test&profile=developer&role=ELITE&key=manager
Output:
username: test
pwd: test
profile: developer
role: ELITE
key: manager

Input: http://www.geeksforgeeks.com/signin/serviceusername=Vikas&pwd=1@@2&profile=developer&role=SoftwareDeveloper&key=Assitant

Output:
username: Vikas
pwd: 1@@2
profile: developer
role: SoftwareDeveloper
key: Assitant



Approach: 

  • Firstly, Remove the web Link from the given URL using the split method.
  • Secondly, Split the URL where “&” operator is Found.
  • In the end, Replace each index value from “=” to “: “.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Get Credential
// Information From the URL(GET Method)
import java.util.*;
import java.io.*;
public class ExchangeCharacter {
  
    public static void main(String args[]) throws Exception
    {
  
        BufferedReader scan = new BufferedReader(
            new InputStreamReader(System.in));
        
          // taking url as a string
        String url
        String str[] = url.split("\\?");
        String arr[] = str[1].split("&");
        for (String s : arr) {
            System.out.println(s.replace("=", ": "));
        }
    }
}

chevron_right


Output:
 

username: Vikas
pwd: 1@@2
profile: developer
role: SoftwareDeveloper
key: Assitant

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.