Files
yanzuoguang 595e040dcb feat(http): 添加HTTP客户端示例和服务器端Cookie处理- 实现多个HTTP客户端请求示例,包括GET、POST表单和JSON数据
- 添加服务器端Cookie处理功能,支持计数器Cookie
- 引入JSON编解码、表单数据发送和Cookie Jar管理
- 实现响应体读取、状态码检查和错误处理逻辑
- 添加多组测试函数用于演示不同的HTTP操作场景
- 支持Cookie的设置、读取和客户端持久化存储
2025-10-26 21:44:51 +08:00

40 lines
983 B
Go

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
func init() {
http.HandleFunc("/html", func(writer http.ResponseWriter, request *http.Request) {
http.ServeFile(writer, request, "index.html")
})
http.HandleFunc("/json", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(writer).Encode(ProductList)
})
http.HandleFunc("/echo", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(writer, "Method: %v \n", request.Method)
for header, vals := range request.Header {
fmt.Fprintf(writer, "Header: %v : %v \n", header, vals)
}
fmt.Fprintln(writer, "-------")
data, err := io.ReadAll(request.Body)
if err != nil {
fmt.Fprintf(writer, "Error reading body: %v \n", err.Error())
return
}
if len(data) == 0 {
fmt.Fprintln(writer, "No body")
} else {
writer.Write(data)
}
})
}