packagemain import( "net/url" "net/http" "bytes" "fmt" "io/ioutil" ) constProxyServer="ip.hahado.cn:39010" typeProxyAuthstruct{ Licensestring SecretKeystring } func(pProxyAuth)ProxyClient()http.Client{ proxyURL,_:=url.Parse("http://"+p.License+":"+p.SecretKey+"@"+ProxyServer) returnhttp.Client{Transport:&http.Transport{Proxy:http.ProxyURL(proxyURL)}} } funcmain(){ targetURI:="http://ip.hahaod.cn/ip" //targetURI:="http://ip.hahaod.cn/switch-ip" //targetURI:="http://ip.hahaod.cn/current-ip" //初始化proxyhttpclient client:=ProxyAuth{License:"username",SecretKey:"password"}.ProxyClient() request,_:=http.NewRequest("GET",targetURI,bytes.NewBuffer([]byte(``))) //切换IP(只支持HTTP) request.Header.Set("Proxy-Switch-Ip","yes") response,err:=client.Do(request) iferr!=nil{ panic("failedtoconnect:"+err.Error()) }else{ bodyByte,err:=ioutil.ReadAll(response.Body) iferr!=nil{ fmt.Println("读取Body时出错",err) return } response.Body.Close() body:=string(bodyByte) fmt.Println("ResponseStatus:",response.Status) fmt.Println("ResponseHeader:",response.Header) fmt.Println("ResponseBody:\n",body) } }
标签:华科云商,http,cn,示例,ip,fmt,golang,targetURI,response From: https://www.cnblogs.com/huakexiaopeng/p/16824621.html