go 伪造User-Agent 抓取网页代码

180it 2020-10-26 PM 1911℃ 0条
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
)

func main() {
    client := &http.Client{
        Timeout: 2 * time.Second,
    }

    req, _ := http.NewRequest("GET", "http://www.xxxx.vip", nil)
    req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36")

    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("%s\n", err.Error())
        return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Printf("%s\n", err.Error())
        return
    }
    fmt.Printf("%s\n", string(body))

}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

go 伪造User-Agent 抓取网页代码