How do I parse a web URL?

Jerry Dodge picture Jerry Dodge · May 23, 2013 · Viewed 8.7k times · Source

I need to be able to break a URL down into different segments. Take this path for example:

http://login:[email protected]:8080/some_path/something_else.html?param1=val&param2=val#nose
\__/   \___/ \______/ \_____________________/ \__/\____________________________/ \___________________/ \__/
 |       |      |               |               |                |                        |              |
Scheme Username Password       Host            Port             Path                    Query         Fragment

This should break down as follows:

Protocol: HTTP
Username: login
Password: password
Host: somehost.somedomain.com
Port: 8080
Path Info: /some_path/something_else.html
Query String: param1=val&param2=val

How can I do this in Delphi? Is there something ready made which can split this up for me? If not, how do I go about parsing all the different possible formats? This is assuming that it might even be a different protocol, such as HTTPS or RTSP.

Answer

Remy Lebeau picture Remy Lebeau · May 23, 2013

XE2 ships with Indy, which has a TIdURI class for that purpose, eg:

uses
  ..., IdURI;

var
  URI: TIdURI;

URI := TIdURI.Create('http://login:[email protected]:8080/some_path/something_else.html?param1=val&param2=val');
try
  // Protocol = URI.Protocol
  // Username = URI.Username
  // Password = URI.Password
  // Host = URI.Host
  // Port = URI.Port
  // Path = URI.Path
  // Query = URI.Params
finally
  URI.Free;
end;