扣丁书屋

Golang 发送http请求时设置header

1年以前  |  阅读数:97 次  |    

package main
import (
    "fmt"
    "io/ioutil"
    "net/http"                                                                                                                                                     
    "os"
    "encoding/json"
)

func main() { //生成client 参数为默认
    client := &http.Client{}
    //生成要访问的url
    url := "http://somesite/somepath/"
    //提交请求
    reqest, err := http.NewRequest("GET", url, nil)

    //增加header选项
    reqest.Header.Add("Cookie", "xxxxxx")
    reqest.Header.Add("User-Agent", "xxx")
    reqest.Header.Add("X-Requested-With", "xxxx")

    if err != nil {
        panic(err)
    }   
    //处理返回结果
    response, _ := client.Do(reqest)
    defer response.Body.Close()
    // ...
}

相关文章: