帮助文档>代码示例 > Go语言代码示例

Go语言代码示例

发布时间:2022-07-03 00:13
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. )
  9.  
  10. func main() {
  11. authKey := "895314XY"
  12. password := "24D6YB309ZCB"
  13. proxyServer := "219.151.125.106:31615"
  14. targetURL := "https://myip.ipip.net"
  15.  
  16. rawURL := fmt.Sprintf("http://%s:%s@%s", authKey, password, proxyServer)
  17. proxyUrl, err := url.Parse(rawURL)
  18. if err != nil {
  19. panic(err)
  20. }
  21.  
  22. client := http.Client{
  23. Transport: &http.Transport{
  24. Proxy: http.ProxyURL(proxyUrl),
  25. },
  26. }
  27.  
  28. req, _ := http.NewRequest("GET", targetURL, nil)
  29. rsp, err := client.Do(req)
  30. if err != nil {
  31. fmt.Printf("request failed: %s\n", err)
  32. return
  33. }
  34. defer rsp.Body.Close()
  35.  
  36. body, err := ioutil.ReadAll(rsp.Body)
  37. if err != nil {
  38. fmt.Println(err)
  39. } else {
  40. fmt.Println(string(body))
  41. }
  42. }
本文导读