go http库 设置代理

func ProxyTest() {
	proxyAddr := "http://your IP:8080/"

	httpUrl := "http://your target url"

	poststr := "your  post values"

	proxy, err := url.Parse(proxyAddr)
	if err != nil {
		log.Fatal(err)
	}

	netTransport := &http.Transport{
		Proxy:                 http.ProxyURL(proxy),
		MaxIdleConnsPerHost:   10,
		ResponseHeaderTimeout: time.Second * time.Duration(5),
	}

	httpClient := http.Client{
		Timeout:   time.Second * 10,
		Transport: netTransport,
	}

	res, err := http.NewRequest("POST", httpUrl, strings.NewReader(poststr))
	if err != nil {
		log.Println(err)
		return
	}

	res.Header.Add("content-type", "application/x-ndjson")

	resp, err := httpClient.Do(res)

	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		log.Println(err)
	}

	c, _ := ioutil.ReadAll(resp.Body)

	fmt.Println(string(c))
}

参考

https://i6448038.github.io/2017/11/11/httpAndGolang/

你可能感兴趣的:(go http库 设置代理)