golang net/http包设置host 2015-08-13 | pyinx | golang 1. 通过req.Header.Set或者req.Header.Add设置host不生效123456789101112client := &http.Client{}req, _ := http.NewRequest("GET", url, nil)req.Header.Set("HOST", "www.baidu.com")res, err := client.Do(req)if err != nil { panic(err)} _, err = ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { panic(err)} 2. 解决123456789101112client := &http.Client{}req, _ := http.NewRequest("GET", url, nil)req.Host = "www.baidu.com"res, err := client.Do(req)if err != nil { panic(err)} _, err = ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { panic(err)}