Start learning free

LESSON 1 OF 173 · BACKEND: HOW THE WEB WORKS

Your First Request

Practice

Every time a browser opens a page, it sends a request to a server, a program waiting on some computer, and the server sends back a response. A C# program can do the same with an HttpClient: the starting code already makes one, called http.

await http.GetAsync(address) sends the request and gives you the response. A response starts with a status code, a number that says how it went (200 means OK), and then comes the body, the content itself. (int)response.StatusCode gives the number, and await response.Content.ReadAsStringAsync() gives the body as a string.

var page = await http.GetAsync("https://example.com/about");
Console.WriteLine((int)page.StatusCode);
Console.WriteLine(await page.Content.ReadAsStringAsync());

The shop's supplier runs a server at the address in SUPPLIER. Its home page is the path /. When your program asks for it, it must print:

Status: 200
Text: Supplier API. Try /prices

Your task: send a GET request to the supplier's home page, keep the answer in response, then print its status code and its text, each after its label.


Assignment
Follow the Coding Tutorial to send your first request and print the status code and the text that come back.


Hint
The address is a string: join SUPPLIER and the path with +. (int) turns the status into a number, and + joins each label with its value.


STEP 1 OF 3
    Ctrl + Enter to check