如何正确使用代理服务器?

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

How to use proxies correctly?

问题

我使用库发送了一个请求并绕过了Cloudflare。我想使用代理,但是如果像通常那样添加代理,程序会崩溃。

headers := map[string]string{
    "Accept":     "*/*",
    "User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
}
proxyStr := "http://101.68.61.82:8085"
proxyURL, _ := url.Parse(proxyStr)
transport := &http.Transport{
    Proxy: http.ProxyURL(proxyURL),
}
client := http.Client{Timeout: 5 * time.Second, Transport: transport}
client.Transport = cloudflarebp.AddCloudFlareByPass(client.Transport)
req, _ := http.NewRequest("GET", "https://api.openproxy.space/lists/http", nil) 
for k, v := range headers {
    req.Header.Set(k, v)
}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))

错误信息:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x40 pc=0x38f96d]

goroutine 1 [running]:
main.requesti()
     C:/Users/user/Desktop/go/parser/test.go:33 +0x40d
main.main()
    C:/Users/user/Desktop/go/parser/test.go:13 +0x17
exit status 2

如何正确添加代理?如果移除代理的添加,一切都能正常工作。

33行:defer resp.Body.Close()
13行:func call

英文:

I send a request and bypass cloudflare using the library. I want to use a proxy, but if I add a proxy as usual, the program crashes.

headers := map[string]string{
	"Accept":     "*/*",
	"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
}
proxyStr := "http://101.68.61.82:8085"
proxyURL, _ := url.Parse(proxyStr)
transport := &http.Transport{
	Proxy: http.ProxyURL(proxyURL),
}
client := http.Client{Timeout: 5 * time.Second, Transport: transport}
client.Transport = cloudflarebp.AddCloudFlareByPass(client.Transport)
req, _ := http.NewRequest("GET", "https://api.openproxy.space/lists/http", nil) 
for k, v := range headers {
	req.Header.Set(k, v)
}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))

error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x40 pc=0x38f96d]

goroutine 1 [running]:
main.requesti()
     C:/Users/user/Desktop/go/parser/test.go:33 +0x40d
main.main()
    C:/Users/user/Desktop/go/parser/test.go:13 +0x17
exit status 2

How do I add a proxy correctly? If I remove the addition of the proxy, everything works perfectly

33 line: defer resp.Body.Close()
13 line: func call

答案1

得分: 0

这个库没有解决方案。请使用其他方法。

英文:

There is no solution using this library. Use other ways.

huangapple
  • 本文由 发表于 2022年9月12日 00:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/73681081.html
匿名

发表评论

匿名网友

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

确定