func client() *http.Client {
//判断url是否有效
//没有http://开头,就加上http://
if !strings.HasPrefix(feed, "http") {
feed = "http://" + feed
}
//判断url是否合理
host, err := url.ParseRequestURI(feed)
if err != nil {
}
//判断是否能解析到对应的host记录
_, err = net.LookupIP(host.Host)
if err != nil {
}
//向主机请求数据
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(10 * time.Second)
c, err := net.DialTimeout(netw, addr, 5*time.Second) //连接超时时间
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
return c, nil
},
},
}
req, err := http.NewRequest("GET", feed, nil)
if err != nil {
}
//数据传输压缩
//告诉主机 支持gzip 数据请求回来后 ungzip
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; UJCspider/0.1; +http://ujiecao.com/help)")
req.Header.Set("Accept-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
}
defer resp.Body.Close()
var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, _ = gzip.NewReader(resp.Body)
defer reader.Close()
default:
reader = resp.Body
}
}
Created
June 8, 2020 07:47
-
-
Save Linuxpizi/018617ae6d150a0cbc3b5b4804d579a6 to your computer and use it in GitHub Desktop.
http client
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment