golang post用户名密码登陆的巨坑

栏目: Go · 发布时间: 7年前

  1. http的Header设置,按照官网或者大部分文章的设置来就可以,如下:
client := &http.Client{Transport: nil}
    req, err := http.NewRequest("POST", post_login_info_url, strings.NewReader(string(info)))
    if err != nil {
       panic(err)
    }
    //header
    req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36")
    req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  1. golang的http form data数据设置,golang.com官网的例子和网上大部分的文章使用url.Values最后提交的时候数据不能到达后台。 最后实在没办法,通过http client的transparent结合charles抓包,仔细对比网页直接请求和 go 请求的差异才把问题解决。 请求的form表单数据是一段json数据,而url.Values不行
  • 错误的情况
postValues := url.Values{}
    postValues.Add("expire", "7")
    postValues.Add("referer", "http%3A%2F%2Fwww.jikexueyuan.com%2F")
    postValues.Add("uname", username)
    postValues.Add("password", password)
    postValues.Add("verify", verify_code)
req, err := http.NewRequest("POST", post_login_info_url, strings.NewReader(postValue.Encode()))
  • 正确的姿势
loginInfo := LoginInfo{
        Username: username,
        Password: password,
        LoginYzm: "",
    }

    info, err := json.Marshal(loginInfo)
req, err := http.NewRequest("POST", post_login_info_url, strings.NewReader(string(info)))

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Introduction to Computation and Programming Using Python

Introduction to Computation and Programming Using Python

John V. Guttag / The MIT Press / 2013-7 / USD 25.00

This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具