使用伪造身份或用户代理的HTTP请求

huangapple go评论84阅读模式
英文:

HTTP request with fake identity or user agent

问题

我想使用Go发送一个HTTP请求。有没有办法可以手动修改我的身份?我希望在发送HTTP请求时使用固定的IP地址和用户代理。

英文:
http.Get("http://Google.com")

I want to send a HTTP request with Go. Is there any way that I can manually modify my identity? I want to have fixed IP addresses and user agents when sending HTTP requests.

答案1

得分: 6

你无法“伪造”你的 IP 地址,但你可以在另一台主机上使用代理。

然而,你可以伪造用户代理:

client := &http.Client{}
req, err := http.NewRequest("GET", "http://google.com", nil)
req.Header.Add("User-Agent", `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.27 Safari/537.36`)
resp, err := client.Do(req)

如果你有代理,你可以这样创建客户端:

purl, err := url.Parse("http://444.555.666.777:8888")
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(purl)}}
英文:

You can't "fake" your ip, you can use a proxy on another host.

However, you can fake the user agent:

client := &http.Client{}
req, err := http.NewRequest("GET", "http://google.com", nil)
req.Header.Add("User-Agent", `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.27 Safari/537.36`)
resp, err := client.Do(req)

If you do have a proxy you can create the client like this instead:

purl, err := url.Parse("http://444.555.666.777:8888")
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(purl)}}

答案2

得分: 1

如果你想定义用户代理,就像OneOfOne所说的那样:

client := &http.Client{}
req, err := http.NewRequest("GET", "http://google.com", nil)
req.Header.Add("User-Agent", `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.27 Safari/537.36`)
resp, err := client.Do(req)

关于IP部分,如果你想更改显示的IP,你需要使用代理。

你可以在Go端进行IP欺骗,但这会使包返回到该地址,这可能不是你的地址。

请更具体地说明你所说的“我想要固定的IP地址”,我会修改我的回答以更好地回答这部分。

英文:

If you want to define the user-agent, just do as OneOfOne said:

client := &http.Client{}
req, err := http.NewRequest("GET", "http://google.com", nil)
req.Header.Add("User-Agent", `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.27 Safari/537.36`)
resp, err := client.Do(req)

regarding to ip part, if you want to change the ip shown, you'll have to use a proxy.

You could do something called ip spoofing on the Go side but that would make the packages return to that address, which might not be yours.

Try to be more specific in respect to what you mean by "I want to have fixed IP addresses" and I'll change my answer to better answer that part.

huangapple
  • 本文由 发表于 2014年10月23日 09:41:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/26519975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定