go语言使用代理ip来访问

作者: ljh123 | 来源:发表于2018-11-27 07:29 被阅读13次
    package main
    
    import (
        "net/url"
        "github.com/lunny/log"
        "net/http"
        "time"
        "fmt"
        "io/ioutil"
    )
    
    func main() {
        proxyAddr := "http://125.46.0.62:53281/"
        httpUrl := "http://134.175.165.18:8000/get_ip"
        proxy, err := url.Parse(proxyAddr)
        if err != nil {
            log.Fatal(err)
        }
        netTransport := &http.Transport{
            Proxy:http.ProxyURL(proxy),
            MaxIdleConnsPerHost: 10,
            ResponseHeaderTimeout: time.Second * time.Duration(5),
        }
        httpClient := &http.Client{
            Timeout: time.Second * 10,
            Transport: netTransport,
        }
        res, err := httpClient.Get(httpUrl)
        if err != nil {
            log.Println(err)
            return
        }
        defer res.Body.Close()
        if res.StatusCode != http.StatusOK {
            log.Println(err)
            return
        }
        c, _ := ioutil.ReadAll(res.Body)
        fmt.Println(string(c))
    }
    

    相关文章

      网友评论

        本文标题:go语言使用代理ip来访问

        本文链接:https://www.haomeiwen.com/subject/xajwqqtx.html