天天看點

go語言使用代理ip來通路

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