Java – can you share a link to a URL parsing implementation?

As far as I know, the URL consists of the following fields:

>Protocol (HTTP, HTTPS, FTP, etc.) > user name > user password > host address (IP address or DNS FQDN) > port (can be implied) > path of documents in the server > file root > collection of parameters and values > file part (#)

as

protocol://user:password@host:port/path/document?arg1=val1&arg2=val2#part

I need a code to get the value of any of these fields from any given URL string (or null / null value, if not set) Do I implement this myself, or do I already have code, so I don't need to invent a wheel?

I'm particularly interested in scala or Java code C #, PHP, python or Perl code is also useful

Solution

The URL class gives you everything you need

URL url = new URL("protocol://user:password@host:port/path/document?arg1=val1&arg2=val2#part");
url.getProtocol();
url.getUserInfo();
url.getAuthority();
url.getHost();
url.getPort();
url.getPath(); // document part is contained within the path field
url.getQuery();
url.getRef(); // gets #part
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>