Start learning free

LESSON 2 OF 173 · BACKEND: HOW THE WEB WORKS

The Parts of a URL

Practice

A URL is the full address of something on the web. It is made of parts, and each part answers its own question: the scheme (how to talk to the server), the host (which computer), the port (which server on that computer), the path (what you want) and the query string (with which options, after the ?).

In C#, new Uri(text) splits a URL into those parts, and each part is a property of the result. Here it is on another URL:

var site = new Uri("https://algocademy.com/blog?page=2");
Console.WriteLine(site.Host);    // algocademy.com
Console.WriteLine(site.Port);    // 443: no port is written, so the usual one
Console.WriteLine(site.Query);   // ?page=2

Scheme, host and port tell your program where to send the request. The path and the query travel inside the request and tell the server what you want. When a URL writes no port, the client uses 80 for http and 443 for https, and Uri gives that number too.

The starting code builds url for the supplier's cheap products and prints it. Your finished program must print:

URL: http://127.0.0.1:8001/prices?max_price=5
Scheme: http
Host: 127.0.0.1
Port: 8001
Path: /prices
Query: max_price=5

Your task: make a new Uri from url and keep it in parts, then print its scheme, host, port, path and query, each after its label.


Assignment
Follow the Coding Tutorial to split the supplier's URL into its five parts and print each one.


Hint
The parts are the properties .Scheme, .Host, .Port, .AbsolutePath and .Query. .Query starts with the ?: .TrimStart('?') removes it.


STEP 1 OF 5
    Ctrl + Enter to check