I faced with weird behavior of Do
function in http library. In my program I have a worker reading a channel. On each message worker calls a function which makes a http request. Here are the function:
func FetchUrlWithProxy(url string, proxy string) (*http.Response, error) {
proxyUrl, err := urllib.Parse(proxy) // [1]
if err != nil {
return nil, err
}
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
log.Println("try ", url) // [2]
response, err := client.Do(request)
log.Println("done ", url) // [3]
if err != nil { // [4]
return nil, err
}
return response, nil
}
[1]
- it's just net/url
imported this this name. [2]
- this line is printed eveytime. [3]
- this line is never printed without any errors/panics. [4]
- so this checking of return value is useless. Execution doesn't reach this point. This behavior definitely is not what I expected. But if I remove proxy usage and create client
as client := &http.Client{}
it will work. On other hand the proxy is not broken. I checked it using curl
. How can it be?
I will provide any additional information if you need.
Your code seems to work - I would suspect that there is something happening with the proxy that you are actually using ?
code :
package main
import (
"log"
"net/http"
urllib "net/url"
)
func FetchUrlWithProxy(url string, proxy string) (*http.Response, error) {
proxyUrl, err := urllib.Parse(proxy) // [1]
if err != nil {
return nil, err
}
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
log.Println("try ", url) // [2]
response, err := client.Do(request)
log.Println("done ", url) // [3]
if err != nil { // [4]
return nil, err
}
return response, nil
}
func main() {
res, err := FetchUrlWithProxy("https://www.google.com", "https://www.google.com")
log.Printf("R %v E %v\n", res, err)
}
And the output :
➜ go run p.go
2015/07/20 21:44:45 try https://www.google.com
2015/07/20 21:44:46 done https://www.google.com
2015/07/20 21:44:46 <nil> Get https://www.google.com: unexpected EOF
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.