开发者

Could you share a link to an URL parsing implementation? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 1 year ago.

Improve this question

As far as I understand, an URL consists of the folowing fields:

  • Protocol (http, https, ftp, etc.)
  • User name
  • User Password
  • Host address (an IP address or a DNS FQDN)
  • Port (which can be implied)
  • Path to a document inside the server documents root
  • Set of arguments and values
  • Document part (#)

as

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

I need a code to get value (or null/empty value if not set) of any of these fields from any given URL string. Am I to implement this myself or there is already a code for this so I don't need to invent a wheel?

I am particularly interested in Scala or Java code. C#, PHP, Python or Perl code can also be开发者_如何学编程 useful.


The URL class gives you everything you need. See http://download.oracle.com/javase/6/docs/api/java/net/URL.html

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


Use the java.net.URI class for this. URLs are for real resources and real protocols. URIs are for possibly non-existent protocols and resources.


In Java, just use the URL class. It provides methods such as getProtocol, getHost, etc. to obtain the different parts of the URL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜