Polling Websites and Reading Web Page
Explore how to send HTTP requests to poll websites and read web page content using Go's net/http package. Learn to handle errors, interpret HTTP status codes, and work with response headers. This lesson prepares you to build simple web applications by mastering essential web communication techniques in Go.
We'll cover the following...
We'll cover the following...
Introduction
Sending a website a very simple request and seeing how the website responds is known as polling a website.
Explanation
Consider the following example where a request includes only an HTTP header.
In the program above:
- Line 4: We import the package
net/http. - Lines 7-11: All URLs defined in an array of strings
urlsare polled. - Lines 16-22: We start an iteration over
urlswith a for-range loop.- Line 17: A simple
http.Head()request is sent to each url to see how they react. The function’s signature is:func Head(url string) (r *Response, err
- Line 17: A simple