前端之家收集整理的这篇文章主要介绍了
Golang 用指定网卡的IP发送HTTP请求,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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,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)
}