Home > Article > Backend Development > Use the http.Client function to send customized HTTP requests and obtain the response status code and response content, and set the timeout and retry times.
Use the http.Client function to send customized HTTP requests and obtain the response status code and response content, and set the timeout and number of retries
When developing web applications, we often need to interact with other services Communicating, sending HTTP requests and getting responses is one of the common scenarios. The http package is provided in the Go language to support the processing of HTTP requests and responses. Among them, http.Client is the core component for sending requests. It provides rich functions to customize HTTP requests and can set timeouts and retries.
Below we use a simple example to illustrate how to use http.Client to send a customized HTTP request and obtain the response status code and response content.
package main import ( "fmt" "net/http" "time" ) func main() { // 创建一个http.Client对象 client := &http.Client{ Timeout: time.Second * 10, // 设置超时时间为10秒 } // 创建一个http.Request对象 req, err := http.NewRequest("GET", "http://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() // 输出响应状态码 fmt.Println("响应状态码:", resp.StatusCode) // 读取响应内容 body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("读取响应失败:", err) return } // 输出响应内容 fmt.Println("响应内容:", string(body)) }
In the above example, we first created an http.Client object and set the timeout to 10 seconds. Then, we create an http.Request object and send it to "http://example.com" using the GET method. Finally, we call the Do method of http.Client to send the request and get the response.
After obtaining the response, we first output the response status code, and then read the response content by calling the ReadAll method of the ioutil package. Finally, we output the response content as a string.
This is just a simple example. Actual development may require more complex HTTP requests, such as request headers, request parameters, request bodies, etc. http.Client provides corresponding methods to set these request attributes, such as AddHeader, SetBasicAuth, SetCookie, etc.
In addition, we can also set the Transport attribute of http.Client to customize the behavior of the HTTP transport layer, such as setting proxy, TLS configuration, etc. To implement the timeout and retry functions, we can use context.Context with the WithContext method of http.Request to set the context of the request, and cancel the request when the timeout or the number of retries reaches the set value.
To sum up, it is a common requirement to use http.Client to send customized HTTP requests and obtain the response status code and response content. By properly setting the properties of http.Client, we can flexibly handle various HTTP requirements and provide a high-quality user experience. I hope the examples in this article can help you deepen your understanding and application of http.Client.
The above is the detailed content of Use the http.Client function to send customized HTTP requests and obtain the response status code and response content, and set the timeout and retry times.. For more information, please follow other related articles on the PHP Chinese website!