共计 1336 个字符,预计需要花费 4 分钟才能阅读完成。
GO 语言代理用法
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
//sock5 代理
func socksproxy() {urli := url.URL{}
urlproxy, _ := urli.Parse("http:// 测试 ip: 端口")
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(urlproxy),
},
}
rqt, err := http.NewRequest("GET", "http://myip.top", nil)
if err != nil {println("接口获取 IP 失败!")
return
}
rqt.Header.Add("User-Agent", "Lingjiang")
// 解决返回后果
response, _ := client.Do(rqt)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {return}
fmt.Println("socks5:", string(body))
return
}
//http 代理
func httpproxy() {urli := url.URL{}
urlproxy, _ := urli.Parse("http:// 测试 ip: 端口")
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(urlproxy),
},
}
rqt, err := http.NewRequest("GET", "http://myip.top", nil)
if err != nil {println("接口获取 IP 失败!")
return
}
rqt.Header.Add("User-Agent", "Lingjiang")
// 解决返回后果
response, _ := client.Do(rqt)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {return}
fmt.Println("http:", string(body))
return
}
// 本机 IP
func httplocal() {client := &http.Client{}
rqt, err := http.NewRequest("GET", "http://myip.top", nil)
if err != nil {println("接口获取 IP 失败!")
return
}
rqt.Header.Add("User-Agent", "Lingjiang")
// 解决返回后果
response, _ := client.Do(rqt)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {return}
fmt.Println("本机:", string(body))
return
}
func main() {httplocal()
httpproxy()
socksproxy()}
正文完