My file is read then broken into lines, and then a random value should be chosen, that is, a proxy server.

Code:

func randInt(len int) int { rand.Seed(time.Now().Unix()) return rand.Intn(len) } func main() { bs, err := ioutil.ReadFile("proxy.txt") if err != nil { return } str := string(bs) randoms := strings.Split(str, "\r\n") bestProxyServer := randInt(len(randoms)) fmt.Print(randoms) proxyUrl, err := url.Parse(randoms[bestProxyServer]) httpClient := &http.Client { Transport: &http.Transport { Proxy: http.ProxyURL(proxyUrl) } } response, err := httpClient.Get(host) } 

I only have a line cut and headers where my usual IP address is specified:

 [110.5.98.234:3128 110.8.253.100:80 113.6.255.107:80 114.30.47.10:80 114.30.79.62:8080 115.124.67.54:80 116.68.172.35:80 80 116.84.187.131:3128]map[Date:[Mon, 06 Nov 2017 15:59:37 GMT] Content-Type:[text/html; charset=UTF-8] Set-Cookie:[PHPS ESSID=tknp6t8h5m6rmptdb8cvef1so2; path=/ IP%5B1509983977%5D=***82.34.213.181***; expires=Sun, 04-Feb-2018 15:59:37 GMT; Max-Ag e=7776000; path=/; domain=.2ip.ru] Server:[nginx] Connection:[keep-alive] Expires:[Thu, 19 Nov 1981 08:52:00 GMT] Cache- Control:[no-store, no-cache, must-revalidate] Pragma:[no-cache]] 

Why I don’t select a random proxy server, but only a whole slice ?

  • In the real error code, check? fmt.Println(proxyUrl) what shows? - Ainar-G
  • @ Ainar-G fmt.Print (randoms [bestProxyServer]), I'm still not attentive, it works randomly, but for some reason, the connection with the proxy north is not established. - Jack Smith
  • The code looks fine. If you didn’t write http.Get(host) instead of http.Get(host) , then everything should work. - Ainar-G
  • @ Ainar-G yes, httpClient.Get (host) method is written - Jack Smith

1 answer 1

Replace fmt.Print(randoms) with fmt.Print(randoms[bestProxyServer]) .

  • So, it displays a different IP address, that is, it still works randomly, but why don't I get a connection to the proxy server? - Jack Smith