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))
}
|
请发表评论