想当然地以为访问https网站,代理地址也需要配置为https://xx.xx,修改为http://后可以使用。具体代码如下:
fixedURL, err:= url.Parse("http://username:[email protected]:8080")
transport:=&http.Transport{
Proxy:http.ProxyURL(fixedURL),
}
client:=&http.Client{Transport:transport}
cookie是用来识别不同client,所以不同浏览器不同PC对应的cookie是不同的,从其他机器直接复制cookie过来有可能是不可用的。
http.request
中的head
是需要针对实际情况使用的,我在使用中多加了一个gzip
导致服务器无法识别。。。。
golang在json编码时,只识别大写开头的变量,如果和json数据格式要求不一致,可通过json:"s"
来标识;
且借助于标准库json的编解码,定义的结构体可以和json返回数据的结构体不完全一致,json会解析出共有的部分;
如下所示:
type respData struct{
Code int `json:"code"`
Data Data `json:"data"`
}
type Data struct{
ApName string `json:"apName"`
FailReason string `json:"failReason"`
RunTime int `json:"runTime"`
}
对应json返回数据格式:
{
"code": 0,
"message": "",
"data":{
"apSN": "219801A0WA9163Q09539",
"acSN": "210235A1JTB15C000019",
"acName": "WX5540H_1",
"apName": "2",
"apModel": "WTU420H",
"macAddr": "487a-daa0-78e0",
"apGroup": "default-group",
"radioNum": 2,
"softVersion": "E2418",
"status": 1,
"onlineTime": 410,
"runTime": 415,
"ipv4Addr": "5.0.0.55",
"failReason": " Kernel exception reboot\r\n"
}
}
req
和resp
的方式借助于httputil
的DumpResponse
和DumpRequest
函数可以方便的打印出req
和resp
的内容,如下:
temp := make([]byte, 1024)
temp, _ = httputil.DumpResponse(resp, true)
log.Println(string(temp))
Body
内容方式除了直接调用Read
方法,还可以借助于ioutil
库的ReadAll
方法,如下:
body2, _ := ioutil.ReadAll(resp.Body)