一、介绍
http包提供了http客户端和服务端的实现
Get,Head,Post和PostForm函数发出http、https的请求
程序在使用完回复后必须关闭回复的主体
#简单的访问网站,由于没有添加header,访问数据不正确 package main import ( "fmt" "io/ioutil" "net/http" ) func main() { res,err:=http.Get("https://www.baidu.com") if err !=nil{ panic(err) } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body)) }
#建立的client客户端,发起的get请求,在请求体中添加header,然后使用client执行这个请求 package main import ( "fmt" "io/ioutil" "net/http" ) func main() { client:=&http.Client{} res,err:=http.NewRequest("GET","https://www.baidu.com",nil) res.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36") resp,err:=client.Do(res) if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }
二、其他使用方法
#监听ip端口,并建立一个sever,让访问者可以访问指定的文件目录 package main import "net/http" func main() {
//设置访问路由 http.Handle("/",http.FileServer(http.Dir("./"))) http.ListenAndServe(":8080",nil) }
package main import ( "bufio" "fmt" "github.com/axgle/mahonia" "golang.org/x/net/html/charset" "golang.org/x/text/encoding" "io" "io/ioutil" "net/http" "strconv" ) func main() { client := &http.Client{} request, err := http.NewRequest("GET", "http://www.baidu.com", nil) if err != nil { fmt.Println(err) } //建立cookie对象 cookie := &http.Cookie{Name: "maple", Value: strconv.Itoa(123)} request.AddCookie(cookie) //向request中添加cookie //设置request的header request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3") request.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") request.Header.Set("Cache-Control", "no-cache") request.Header.Set("Connection", "keep-alive") response, err := client.Do(request) if err != nil { fmt.Println(err) return } e:=determineEncoding(response.Body) fmt.Println("当前编码:",e) defer response.Body.Close() if response.StatusCode == 200 { r, err := ioutil.ReadAll(response.Body) if err != nil { fmt.Println(err) } //编码转换,如编码不正确可转成指定编码 srcCoder :=mahonia.NewEncoder("utf-8") res:=srcCoder.ConvertString(string(r)) fmt.Println(res) } } //编码检测 func determineEncoding(r io.Reader) encoding.Encoding { bytes, err := bufio.NewReader(r).Peek(1024) if err != nil { panic(err) } e, _, _ := charset.DetermineEncoding(bytes, "") return e }