在golang发表帖子请求

时间:2016-01-06 00:09:32

标签: go

我正在尝试使用golang自动执行

  • 生成密码(已完成)
  • 向所有设备推送通知(使用pushbullet完成)
  • 将wifi路由器密码更改为192.168.0.1(需要完成)

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

以下是使用 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)
}

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

1 个答案:

答案 0 :(得分:2)

希望这会有所帮助。

有效的代码

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)
}