


Use the net/http.Head function to send a HEAD request and get the response status code
Use the net/http.Head function to send a HEAD request and get the response status code
In the Go language, we can use the functions provided by the net/http package to send HTTP requests and process HTTP responses. Among them, the Head function can send a HEAD request and return the response status code.
The following is a sample code that shows how to use the net/http.Head function to send a HEAD request and get the response status code:
package main import ( "fmt" "net/http" ) func main() { // 创建一个http.Client client := &http.Client{} // 创建一个HEAD请求 req, err := http.NewRequest("HEAD", "https://www.example.com", nil) if err != nil { fmt.Println("创建请求失败:", err) return } // 发送请求 resp, err := client.Do(req) if err != nil { fmt.Println("发送请求失败:", err) return } defer resp.Body.Close() // 获取响应状态码 statusCode := resp.StatusCode fmt.Println("响应状态码:", statusCode) }
In the above sample code, we first create a http.Client, and then uses the http.NewRequest function to create a HEAD request object. Next, we sent the request through the client.Do method and got the response object resp. Finally, we get the response status code through resp.StatusCode and print it out.
It should be noted that the request address in the above code is "https://www.example.com", you can replace it with the specific target URL.
Through the above code example, we can easily use the Head function of the net/http package to send a HEAD request and obtain the response status code. This is very useful in some scenarios where we only care about service availability, response time and other information.
The above is the detailed content of Use the net/http.Head function to send a HEAD request and get the response status code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use the net/http/httputil.DumpResponse function in golang to print HTTP response information In golang, we can use the net/http package to send HTTP requests and receive HTTP responses. Sometimes, we need to view the details of the HTTP response, such as response headers, response body, etc. To achieve this purpose, the net/http/httputil package provides a useful DumpResponse function.

Overview of how to use the net/http/httputil.DumpRequest function in golang to print HTTP request information: In Golang, you can use the httputil.DumpRequest function provided by the net/http package to print HTTP request information. This function can help us easily view the contents of the request header, request line, and request body. This article details how to use this function and provides specific code examples. Step 1: Guide

Today, the Internet has become an extremely precious resource, and data needs to be sent at all times. To stay competitive in this fast-paced era, we need to master a variety of skills. Go language has become a very popular language currently. Sending data has become easier and more efficient in Go. This article introduces the net/http.PostForm function in the Go language documentation to send form data, providing readers with a simple method that allows programmers to run programs quickly and easily. HTTPPOS

Use the net/http.Head function to send a HEAD request and get the response status code. In the Go language, we can use the functions provided by the net/http package to send HTTP requests and process HTTP responses. Among them, the Head function can send a HEAD request and return the response status code. Here is a sample code that shows how to use the net/http.Head function to send a HEAD request and get the response status code: packagemainimport

Go is a fast and easy-to-use programming language with efficient memory management and good concurrency features. The net/http package in its standard library provides the implementation of HTTP client and server, and also provides many HTTP-related functions. Among them, the NewServeMux function is a very commonly used function. It can create an HTTP request router and bind the router to the HTTP server. This article will introduce NewServeMux in the net/http package

In the daily Go language development process, we often need to start an HTTPS server to test some security-related functions, especially when developing web applications. At this time, we can use the Go language's built-in net/http/httptest.NewTLSServer function to create a temporary HTTPS server for testing. So, this article will introduce how to use the NewTLSServer function to start a temporary HTTPS server and also

Learning network programming in Go language is a very important part, and sending POST requests is an indispensable part. This article will introduce how to use the net/http.Post function in the Go language documentation to send a POST request, including specific code examples. First, we need to understand what a POST request is. It is a request method for sending data to the server. Unlike GET requests, POST requests can send more data and do not expose the data in the URL. Normally, we use P

How to simulate HTTP requests and responses using the net/http/httptest.NewRecorder function in golang When developing and testing web applications, it is often necessary to simulate HTTP requests and responses. Golang provides the net/http/httptest package to easily simulate HTTP requests and responses. Among them, the httptest.NewRecorder function is a very useful function, which can create a
