Last active
March 19, 2025 03:23
-
-
Save kemadz/12a8e60a48ea995bfced4f64751d2881 to your computer and use it in GitHub Desktop.
golang http client proxy sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"log" | |
"net/http" | |
) | |
func main() { | |
// url := "http://httpbin.org/user-agent" | |
url := "https://ip.im/" | |
// proxyURL, _ := url.Parse("http://127.0.0.1:7890") | |
// proxy := http.ProxyURL(proxyURL) | |
// transport := &http.Transport{Proxy: proxy} | |
transport := &http.Transport{Proxy: http.ProxyFromEnvironment} | |
client := &http.Client{Transport: transport} | |
req, _ := http.NewRequest("GET", url, nil) | |
req.Header.Set("User-Agent", "curl/8.12.1") | |
resp, _ := client.Do(req) | |
defer resp.Body.Close() | |
body, _ := io.ReadAll(resp.Body) | |
log.Println(http.ProxyFromEnvironment(req)) | |
log.Println(string(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment