模拟浏览器发起请求
实现get方法
package main
func main(){
// 模拟浏览器
response,err:=http.Get("http://localhost:8888/help")
// 只有这三个方法
//
if err !=nil{
fmt.Println(err)
}
fmt.Println(response.Proto)
fmt.Println(response.StatusCode)
io.Copy(os.Stdout,response.Body)
}
NewRequest函数
包中只有 http.Post、http.Head、http.Get 三个方法,但这三个方法都是NewRequest函数的封装,其他方法可以通过http.NewRequest函数来执行
req,err:=http.NewRequest("DELETE","http://localhost:8888/home",nil) //DELETE位方法名,会返回一个request的指针类型
//发起请求要有一个client端,在http包中是一个结构体,如下
client:=http.Client{}
response,err=client.Do(req)
io.Copy(os.Stdout,response.Body)
//https协议的:
response,err:=http.Get("https://www.baidu.com")
io.Copy(os.Stdout,response.Body)
在访问不受信的https网站时,需要使用原生的request,而不能使用封装好的Get、Post、Head方法
req,err:=http.NewRequest("GET","http://localhost:8888/home",nil)
client:=http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
response,err:=client.Do(req)
文档更新时间: 2021-08-22 18:24 作者:张尚