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))
}
上一篇:Java的wait(), notify()和notifyAll()使用小结


下一篇:网站添加数据出错,原来是MS SQL Server2008日志文件占据空间过大导致的