1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package main
import ( "fmt" "net/http" "net/url" "time"
"golang.org/x/net/proxy" )
func main() { proxyStr := "socks5://127.0.0.1:1080"
proxyURL, err := url.Parse(proxyStr) if err != nil { fmt.Println("Error parsing proxy URL:", err) return }
dialer, err := proxy.FromURL(proxyURL, proxy.Direct) if err != nil { fmt.Println("Error creating dialer:", err) return }
transport := &http.Transport{ Dial: dialer.Dial, }
client := &http.Client{ Transport: transport, Timeout: 5 * time.Second, }
url := "https://www.baidu.com" req, err := http.NewRequest("GET", url, nil) if err != nil { fmt.Println("创建连接错误:", err) return }
startTime := time.Now() resp, err := client.Do(req) if err != nil { fmt.Println("发送请求错误:", err) return } defer resp.Body.Close() elapsedTime := time.Since(startTime) fmt.Println("连接地址:", url, "延迟时间:", elapsedTime) }
|