1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package main
import ( "fmt" "io" "net/http" "strings" )
func main() { url := "https://httpbin.org/post" data:=strings.NewReader("This is the data to be submitted!") testClientGet(url,data) } func testClientGet(url string,data *strings.Reader) { client := http.Client{} r,err:=http.NewRequest("POST",url,data) CheckErr(err) res, err := client.Do(r) CheckErr(err) defer res.Body.Close()
if res.StatusCode == 200 { data, err := io.ReadAll(res.Body) CheckErr(err) fmt.Println(string(data)) } else { fmt.Println("网络访问失败!状态码:", res.StatusCode) } } func CheckErr(err error) { defer func() { if ins, ok := recover().(error); ok { fmt.Println("程序异常!", ins.Error()) } }() if err != nil { panic(err) } }
|