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))
}
    原文作者:laijh
    原文地址: https://www.jianshu.com/p/b332ab0e2d7c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞