刚刚写一个自动连接校园网的小脚本,用py写的话很简单,基本20行内就可以解决,但用go写的话,还没写过,
抓包分析,发送的是明文表单数据,userid, password, 以及其他几个参数,
data := map[string]interface{}{
"userId": "1111111",
"password": "999999"
"encrypt": "false",
}
jsonData, _ := json.Marshal(data)
reader := bytes.NewBuffer(jsonData)
resp, _ := http.Post(url_, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8", reader)
这样发送的数据不在表单里,
– 方法一
FormValue
可以处理url
中的query
键值对,对于PUT/POST/PATCH请求,也会将body
中的内容处理成键值对,但Content-Type
要设置成application/x-www-form-urlencoded
,因此只要在request中添加这个header
即可:
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := http.Client{}
resp, err := client.Do(req)
values := url.Values {
"userId": {"000"},
"password": {"000"},
"passwordEncrypt": {"false"},
"queryString": {"22222"},
}
// 设置代理,为了方便抓包,不然抓不到包。
proxyURL, _ := url.Parse("http://127.0.0.1:8889")
trans := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client := &http.Client{
Transport: trans,
}
res, _ := http.NewRequest("POST", url_, strings.NewReader(values.Encode()))
res.Header.Set("User-Agent", "Mozilla/5.0")
res.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(res)
- 方法二
func main() {
postData := make(map[string]string)
postData["anchorId"] = "3---"
postData["searchBegin"] = "20211011"
postData["searchEnd"] = "202211011"
url := ""
PostWithFormData("POST",url ,&postData)
}
func PostWithFormData(method, url string, postData *map[string]string){
body := new(bytes.Buffer)
w := multipart.NewWriter(body)
for k,v := range *postData{
w.WriteField(k, v)
}
w.Close()
req, _ := http.NewRequest(method, url, body)
req.Header.Set("Content-Type", w.FormDataContentType())
resp, _ := http.DefaultClient.Do(req)
data, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
fmt.Println(resp.StatusCode)
fmt.Printf("%s", data)
}