在 golang 中发出 post 请求

我正在尝试使用 golang 自动跟踪

Wifi 路由器页面位于 192.168.0.1 Wifi 路由器页面


这是使用Fiddler捕获的原始数据。(手动更改时)


POST http://192.168.0.1/goform/form2WlanBasicSetup.cgi HTTP/1.1

Host: 192.168.0.1

User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Referer: http://192.168.0.1/d_wlan_basic.asp

Cookie: curShow=

X-Skyzip-Mode: high

Connection: keep-alive

Content-Type: application/x-www-form-urlencoded

Content-Length: 233


domain=1&hiddenSSID=0&ssid=Home&band=9&chan=0&chanwid=1&txRate=0&method_cur=0&method=6&authType=2&length=1&format=1&defaultTxKeyId=1&key1=&pskFormat=0&pskValue=3ADI0NSXEAYOI0M&checkWPS2=1&save=Apply&basicrates=496&operrates=4080

和我的golang代码如下


func RouterPass(pass string) {

    routerUrl := "http://192.168.0.1"

    resource := "/goform/form2WlanBasicSetup.cgi"

    data := url.Values{}

    data.Set("domain", "1")

    data.Add("ssid", "Home")

    data.Add("band", "9")

    data.Add("chan", "0")

    data.Add("chanwid", "1")

    data.Add("txRate", "0")

    data.Add("method_cur", "0")

    data.Add("method", "6")

    data.Add("authType", "2")

    data.Add("length", "1")

    data.Add("format", "1")

    data.Add("defaultTxKeyId", "1")

    data.Add("pskFormat", "0")

    data.Add("pskValue", pass)

    data.Add("checkWPS2", "1")

    data.Add("save", "Apply")

    data.Add("basicrates", "496")

    data.Add("operrates", "4080")


    u, _ := url.ParseRequestURI(routerUrl)

    u.Path = resource

    u.RawQuery = data.Encode()

    urlStr := fmt.Sprintf("%v", u) 


    client := &http.Client{}

    r, _ := http.NewRequest("POST", urlStr, nil)

    // r.Header.Add("Authorization", "auth_token=\"XXXXXXX\"")

    r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

    r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))


    resp, _ := client.Do(r)

    fmt.Println(pass)

    fmt.Println(resp.Status)

}

但我无法更改密码。我究竟做错了什么?



    慕虎7371278
    浏览 134回答 1
    1回答

    莫回无

    希望这会对其他人有所帮助。有效的代码func ChangePassword(password string) {    hc := http.Client{}    routerURL := "http://192.168.0.1/goform/form2WlanBasicSetup.cgi"    form := url.Values{}    form.Add("domain", "1")    form.Add("hiddenSSID", "0")    form.Add("ssid", "Linux-PC")    form.Add("band", "9")    form.Add("chan", "0")    form.Add("chanwid", "1")    form.Add("txRate", "0")    form.Add("method_cur", "0")    form.Add("method", "6")    form.Add("authType", "2")    form.Add("length", "1")    form.Add("format", "1")    form.Add("defaultTxKeyId", "1")    form.Add("key1", "")    form.Add("pskFormat", "0")    form.Add("pskValue", password)    form.Add("checkWPS2", "1")    form.Add("save", "Apply")    form.Add("basicrates", "496")    form.Add("operrates", "4080")    req, err := http.NewRequest("POST", routerURL, strings.NewReader(form.Encode()))    if err != nil {        panic(err)    }    req.PostForm = form    req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0")    req.Header.Add("Referer", "http://192.168.0.1/d_wlan_basic.asp")    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")    fmt.Println(form)    resp, err := hc.Do(req)    if err != nil {        panic(err)    }    fmt.Println(resp.Status)    fmt.Println(password)}
    打开App,查看更多内容
    随时随地看视频慕课网APP

    相关分类

    Go