golang通过代理访问url

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Nick_666/article/details/82596289

直接上实例

命令测试
curl --proxy 10.0.55.22:8080 -v "http://www.baidu.com/"
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "net"
    "time"
)

func main() {
    proxyAddr := "http://10.0.55.22:8080/"
    url := "http://www.baidu.com/"
    cli := NewHttpClient(proxyAddr)
    data,_ := HttpGET(cli, url)
    fmt.Println(string(data))
}


func NewHttpClient(proxyAddr string) *http.Client {
    proxy, err := url.Parse(proxyAddr)
    if err != nil {
        return nil
    }

    netTransport := &http.Transport{
        //Proxy: http.ProxyFromEnvironment,
        Proxy: http.ProxyURL(proxy),
        Dial: func(netw, addr string) (net.Conn, error) {
            c, err := net.DialTimeout(netw, addr, time.Second*time.Duration(10))
            if err != nil {
                return nil, err
            }
            return c, nil
        },
        MaxIdleConnsPerHost:   10,                            //每个host最大空闲连接
        ResponseHeaderTimeout: time.Second * time.Duration(5), //数据收发5秒超时
    }

    return &http.Client{
        Timeout:   time.Second * 10,
        Transport: netTransport,
    }
}

func HttpGET(client *http.Client, url string) (body []byte, err error) {
    rsp, err := client.Get(url)
    if err != nil {
        return
    }
    defer rsp.Body.Close()

    if rsp.StatusCode != http.StatusOK || err != nil{
        err = fmt.Errorf("HTTP GET Code=%v, URI=%v, err=%v", rsp.StatusCode, url, err)
        return
    }

    return ioutil.ReadAll(rsp.Body)
}
展开阅读全文

没有更多推荐了,返回首页