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()
}

goper
413 声望25 粉丝

go 后端开发