


Use the http.Get function to send a GET request and obtain the response status code and response content length.
Use the http.Get function to send a GET request and obtain the response status code and response content length
In the Go language, we can use the http.Get function provided by the http package to send a GET request. Through the GET request, we can obtain the response status code returned by the server and the length of the response content. Let's take a look at the specific implementation process.
First, in our Go code, we need to import the net/http package to use the http.Get function. The code is as follows:
package main import ( "fmt" "net/http" ) func main() { // 发送GET请求 resp, err := http.Get("http://www.example.com") if err != nil { fmt.Println("发送GET请求失败:", err) return } defer resp.Body.Close() // 获取响应状态码 statusCode := resp.StatusCode fmt.Println("响应状态码:", statusCode) // 获取响应内容长度 contentLength := resp.ContentLength if contentLength == -1 { fmt.Println("响应内容长度未知") } else { fmt.Println("响应内容长度:", contentLength) } }
In the above code, we sent a GET request to "http://www.example.com" through the http.Get function, and assigned the response result to the resp variable . At the same time, we use the defer keyword to ensure that the response Body is closed before the function returns to prevent resource leaks.
Next, we use resp.StatusCode to get the response status code and print it out. The response status code represents the server's processing result of the request. It is usually a three-digit integer. For example, 200 indicates that the request was successful, and 404 indicates that the page was not found.
Finally, we use resp.ContentLength to get the length of the response content and print it out. ContentLength returns an int64 type value, indicating the size of the response content. It should be noted that some servers may not provide the Content-Length field in the response header. In this case, ContentLength will return -1, indicating that the response content length is unknown.
With the above code, we can send a GET request and obtain the response status code and response content length. You can modify the code as needed to suit your actual needs. Hope this article is helpful to you!
The above is the detailed content of Use the http.Get function to send a GET request and obtain the response status code and response content length.. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



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

PHP is a popular server-side programming language that is widely used to build web applications. When developing web applications using PHP, it is very important to ensure security. Since HTTP requests include GET and POST, and since the GET request contains the request parameters sent by the client in the URL, the security of the web application can be enhanced by prohibiting the GET request. In this article, we will discuss how to suppress GET requests through PHP.

Use the http.Get function to send a GET request and get the response. In network programming, sending HTTP requests is a very common operation. By sending HTTP requests, we can obtain data on the remote server or interact with it. In the Go language, we can use the http package to send HTTP requests and use the http.Get function to send GET requests and get responses. The http.Get function is a simple function provided in the http package. It is used to send a GET request and return a

Python is a popular programming language widely used in areas such as web development, data analysis, and automation tasks. In the Python2.x version, you can easily send GET requests and obtain response data using the urlopen() function of the urllib library. This article will introduce in detail how to use the urlopen() function to send GET requests in Python2.x, and provide corresponding code examples. Before sending a GET request using the urlopen() function, we first need to

How to send a GET request and parse the response using HTTP client functions in Go language? 1. Introduction to HTTP client functions In the Go language, the standard library provides functions for implementing HTTP clients. These functions can be used to send various types of HTTP requests and parse the responses. In this article, we will focus on how to use HTTP client functions to send a GET request and parse the response. 2. Sending a GET request In the Go language, sending a GET request can be implemented through the http.Get() function.

How to create a simple GET request using FastAPI In modern web development, building RESTful APIs is a very common task. FastAPI is a modern, fast (high-performance) web framework based on Python that provides a concise, easy-to-use way to build efficient APIs. This article will introduce how to create a simple GET request using the FastAPI framework. We will use FastAPI's decorator to route requests and write some simple

Differences: 1. Post requests are more secure; post requests will not be used as part of the URL, will not be cached, and will not be saved in server logs and browser browsing records. If the get request is a static resource, it will be cached. If it is data, will not be cached. 2. The data sent by the post request is larger, and the get request has a URL length limit. 3. The post request can send more data types, while the get request can only send ASCII characters. 4. The methods of parameter transmission are different. 5. Get generates one TCP packet; post generates two.

Use the http.NewRequest function to create a new GET request object and set the request headers and parameters. In the Go language, we can use the http.NewRequest function to create a new GET request object and set the request headers and parameters. The http.NewRequest function accepts three parameters: request method, request URL and request body. We can use the http.MethodGet constant to represent the GET request method and a string to represent the request to be sent.
