Golang通过http.NewRequest实现模拟请求,添加请求头
Golang 通过 http.NewRequest 实现模拟请求,添加请求头和请求参数:
func DownloadString(remoteUrl string,queryValues url.Values) (body []byte,err error){
client := &http.Client{};
body = nil;
uri,err := url.Parse(remoteUrl);
if(err != nil){
return ;
}
if(queryValues != nil){
values := uri.Query();
if(values != nil){
for k,v := range values {
queryValues[k] = v;
}
}
uri.RawQuery = queryValues.Encode();
}
reqest, err := http.NewRequest("GET",uri.String(),nil);
reqest.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
reqest.Header.Add("Accept-Encoding", "gzip, deflate");
reqest.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
reqest.Header.Add("Connection", "keep-alive");
reqest.Header.Add("Host", uri.Host);
reqest.Header.Add("Referer", uri.String());
reqest.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
response, err := client.Do(reqest)
defer response.Body.Close();
if(err != nil){
return ;
}
if response.StatusCode == 200 {
switch response.Header.Get("Content-Encoding") {
case "gzip":
reader, _ := gzip.NewReader(response.Body)
for {
buf := make([]byte, 1024)
n, err := reader.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
body = append(body,buf...);
}
default:
body, _ = ioutil.ReadAll(response.Body)
}
}
return ;
}
func DownloadString(remoteUrl string,queryValues url.Values) (body []byte,err error){
client := &http.Client{};
body = nil;
uri,err := url.Parse(remoteUrl);
if(err != nil){
return ;
}
if(queryValues != nil){
values := uri.Query();
if(values != nil){
for k,v := range values {
queryValues[k] = v;
}
}
uri.RawQuery = queryValues.Encode();
}
reqest, err := http.NewRequest("GET",uri.String(),nil);
reqest.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
reqest.Header.Add("Accept-Encoding", "gzip, deflate");
reqest.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
reqest.Header.Add("Connection", "keep-alive");
reqest.Header.Add("Host", uri.Host);
reqest.Header.Add("Referer", uri.String());
reqest.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
response, err := client.Do(reqest)
defer response.Body.Close();
if(err != nil){
return ;
}
if response.StatusCode == 200 {
switch response.Header.Get("Content-Encoding") {
case "gzip":
reader, _ := gzip.NewReader(response.Body)
for {
buf := make([]byte, 1024)
n, err := reader.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
body = append(body,buf...);
}
default:
body, _ = ioutil.ReadAll(response.Body)
}
}
return ;
}
- 原文作者:知识铺
- 原文链接:https://geek.zshipu.com/post/go/Golang%E9%80%9A%E8%BF%87http.NewRequest%E5%AE%9E%E7%8E%B0%E6%A8%A1%E6%8B%9F%E8%AF%B7%E6%B1%82%E6%B7%BB%E5%8A%A0%E8%AF%B7%E6%B1%82%E5%A4%B41/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。
- 免责声明:本页面内容均来源于站内编辑发布,部分信息来源互联网,并不意味着本站赞同其观点或者证实其内容的真实性,如涉及版权等问题,请立即联系客服进行更改或删除,保证您的合法权益。转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。也可以邮件至 sblig@126.com