http.Request 发送请求
package main
import (
"fmt"
"net/http"
"strings"
)
func main(){
reader := strings.NewReader("hello")
req, _ := http.NewRequest(http.MethodGet,"http://www.baidu.com", reader)
fmt.Printf("00>> reader=%#v,\nreq=%v \n",reader,req)
client := http.Client{}
resp1 ,err := client.Do(req) // 第一次会请求成功
fmt.Printf("01>> resp1=%#v,\nerr=%v \n",resp1,err)
client.Do(req) // 请求失败
resp2 ,err2 := client.Do(req) // 第二次会请求失败
fmt.Printf("02>> resp2=%#v,\nerr2=%v \n",resp2,err2)
}
输出:
00>> reader=&strings.Reader{s:"hello", i:0, prevRune:-1},
req=&{GET http://www.baidu.com HTTP/1.1 1 1 map[] {0xc000004600} 0x6295a0 5 [] false www.baidu.com map[] map[] <nil> map[] <nil> <nil> <nil> 0xc0000120b0}
01>> resp1=&http.Response{Status:"200 OK", StatusCode:200, Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Bdpagetype":[]string{"1"}, "Bdqid":[]string{"0xc763024b00042048"}, "Connecti
on":[]string{"keep-alive"}, "Content-Type":[]string{"text/html; charset=utf-8"}, "Date":[]string{"Sat, 09 Apr 2022 03:37:21 GMT"}, "P3p":[]string{"CP=\" OTI DSP COR IVA OUR IND COM \"", "CP=\" OTI DSP
COR IVA OUR IND COM \""}, "Server":[]string{"BWS/1.1"}, "Set-Cookie":[]string{"BAIDUID=59B296209B882E9342ADE305AC33019F:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.ba
idu.com", "BIDUPSID=59B296209B882E9342ADE305AC33019F; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com", "PSTM=1649475441; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age
=2147483647; path=/; domain=.baidu.com", "BAIDUID=59B296209B882E93B7ED2A807865BEDB:FG=1; max-age=31536000; expires=Sun, 09-Apr-23 03:37:21 GMT; domain=.baidu.com; path=/; version=1; comment=bd", "BDSVR
TM=0; path=/", "BD_HOME=1; path=/", "H_PS_PSSID=35838_36056_36019_36087_36166_34584_36144_36120_36194_35978_36125_35801_36226_26350_36103_36061; path=/; domain=.baidu.com"}, "Traceid":[]string{"1649475
441038878874614367329757388087368"}, "X-Frame-Options":[]string{"sameorigin"}, "X-Ua-Compatible":[]string{"IE=Edge,chrome=1"}}, Body:(*http.gzipReader)(0xc0002040a0), ContentLength:-1, TransferEncoding
:[]string{"chunked"}, Close:false, Uncompressed:true, Trailer:http.Header(nil), Request:(*http.Request)(0xc000134000), TLS:(*tls.ConnectionState)(nil)},
err=<nil>
02>> resp2=(*http.Response)(nil),
err2=Get "http://www.baidu.com": http: ContentLength=5 with Body length 0
写入自己的博客中才能记得长久