在做post请求的时候有302重定向,但发现重定向的时候提示未登录,后来查找了原因发现是golang在处理302重定向的时候没有携带cookies,找了好久才找到了解决办法,直接贴出来代码
req, _ := http.NewRequest("POST", "http://www.baidu.com",
strings.NewReader(fmt.Sprintf("pma_username=root&pma_password=%s", "123123")))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
//这里是重点
jar, _ := cookiejar.New(nil)
client := &http.Client{Jar: jar}
//到这里结束
resp, _ := client.Do(req)
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
fmt.Println(string(body))
前人栽树 后人乘凉