最近在用golang 开发一个web版的rss阅读器,在分析种子是否有效的时候总结了几点跟HTTP有关的优化。

1 判断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 {
}

2 向主机请求数据
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 {
        }

3 数据传输压缩
//告诉主机 支持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
}