package test
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"testing"
)
var proxyConf = "112.195.81.161:8118"
func buildHtppClient(isProxy bool) *http.Client {
var proxy func(*http.Request) (*url.URL, error) = nil
if isProxy {
proxy = func(_ *http.Request) (*url.URL, error) {
return url.Parse("http://" + proxyConf)
}
}
transport := &http.Transport{Proxy: proxy}
client := &http.Client{Transport: transport}
return client
}
func Test_crawler(t *testing.T) {
url := "http://httpbin.org/get"
client := buildHtppClient(true)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
“年轻时,我没受过多少系统教育,但什么书都读。读得最多的是诗,包括烂诗,我坚信烂诗早晚会让我邂逅好诗。”
by. 马尔克斯