import ( "net" "net/http" "time")//url 要请求的URL // ipaddr 当前网卡绑定的IP(一般都是网外IP)func HttpGetFromIP(url, ipaddr string) (*http.Response, error) { req, _ := http.NewRequest("GET", url, nil) client := &http.Client{ Transport: &http.Transport{ Dial: func(netw, addr string) (net.Conn, error) { //本地地址 ipaddr是本地外网IP lAddr, err := net.ResolveTCPAddr(netw, ipaddr+":0") if err != nil { return nil, err } //被请求的地址 rAddr, err := net.ResolveTCPAddr(netw, addr) if err != nil { return nil, err } conn, err := net.DialTCP(netw, lAddr,rAddr) if err != nil { return nil, err } deadline := time.Now().Add(35 * time.Second) conn.SetDeadline(deadline) return conn, nil }, }, } req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36") return client.Do(req)}