Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to parse URL in Java

2 Answers

0 votes
import java.net.URL;
import java.net.MalformedURLException;

public class Main {
    public static void main(String[] args) {
        try {
            String urlStr = "https://www.collectivesolver.com:8080/path/to/resource?query=array#fragment";
            
            URL url = new URL(urlStr);

            // Parsing different components of the URL
            String protocol = url.getProtocol();
            String host = url.getHost();
            int port = url.getPort();
            String path = url.getPath();
            String query = url.getQuery();
            String fragment = url.getRef();

            // Printing the components
            System.out.println("Protocol: " + protocol);
            System.out.println("Host: " + host);
            System.out.println("Port: " + port);
            System.out.println("Path: " + path);
            System.out.println("Query: " + query);
            System.out.println("Fragment: " + fragment);
        } catch (MalformedURLException e) {
            System.out.println("The URL is malformed: " + e.getMessage());
        }
    }
}



/*
run:

Protocol: https
Host: www.collectivesolver.com
Port: 8080
Path: /path/to/resource
Query: query=array
Fragment: fragment

*/

 



answered Jan 31, 2025 by avibootz
0 votes
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        try {
            String urlStr = "https://www.collectivesolver.com:8080/path/to/resource?query=array#fragment";
            URI uri = new URI(urlStr);

            String scheme = uri.getScheme();
            String host = uri.getHost();
            int port = uri.getPort();
            String path = uri.getPath();
            String fragment = uri.getFragment();

            System.out.println("Scheme: " + scheme);
            System.out.println("Host: " + host);
            System.out.println("Port: " + port);
            System.out.println("Path: " + path);
            System.out.println("Fragment: " + fragment);

            String query = uri.getQuery();
            if (query != null) {
                System.out.println("Query: " + query);

                Map<String, String> params = Arrays.stream(query.split("&"))
                        .map(s -> s.split("=", 2)) // Limit split to 2 parts (key and value)
                        .filter(parts -> parts.length >= 1) // Ensure at least a key exists
                        .collect(Collectors.toMap(
                                parts -> URLDecoder.decode(parts[0], StandardCharsets.UTF_8),
                                parts -> parts.length > 1 ? URLDecoder.decode(parts[1], StandardCharsets.UTF_8) : null, // Handle missing value
                                (oldValue, newValue) -> newValue // Handle duplicate keys (choose last value)
                        ));

                params.forEach((key, value) -> System.out.println("Parameter: " + key + " = " + value));
            }

        } catch (URISyntaxException e) {
            System.out.println("The URI is malformed: " + e.getMessage());
        }
    }
}


/*
run:

Scheme: https
Host: www.collectivesolver.com
Port: 8080
Path: /path/to/resource
Fragment: fragment
Query: query=array
Parameter: query = array

*/

 



answered Jan 31, 2025 by avibootz

Related questions

2 answers 224 views
224 views asked Feb 1, 2025 by avibootz
1 answer 111 views
111 views asked Feb 1, 2025 by avibootz
1 answer 76 views
76 views asked Feb 1, 2025 by avibootz
1 answer 108 views
108 views asked Feb 1, 2025 by avibootz
1 answer 75 views
1 answer 75 views
1 answer 44 views
44 views asked Jan 31, 2025 by avibootz
...