简体   繁体   English

使用golang http库提交表单

[英]Submitting form with golang http library

Oke, I'm currently trying to login in to my school website, with my own Crawler. Oke,我目前正在尝试使用自己的Crawler登录我的学校网站。 Altough they have some protection against login. 尽管他们对登录有一定的保护。 First I do a Get request to the Website so I get the token from the hidden Input field. 首先,我向网站发出“获取”请求,以便从隐藏的“输入”字段中获取令牌。 That token I use in my next Post request to login to the url! 我在下一个Post请求中使用该令牌登录URL! But for some reason the http response is that I cannot resubmit the form. 但是由于某种原因,http响应是我无法重新提交表单。 But with doing the same in Postman rest client (chrome plugin) I can login! 但是通过在Postman rest client(chrome插件)中执行相同的操作,我可以登录!

When I try to submit a form to this url: 当我尝试向此网址提交表单时:

postLoginUrl = "?username=%s&password=%s&submit=inloggen&_eventId=submit&credentialsType=ldap&lt=%s"
loginUrl     = "https://login.hro.nl/v1/login"

where %s are filled in credentials %s填写凭据的位置

req, err := client.Post(loginUrl, "application/x-www-form-urlencoded", strings.NewReader(uri))

I'm getting as response that the Form cannot be resubmitted. 我得到的回应是无法重新提交该表格。

But when I try it with Postman rest client, I'm allowed to login. 但是,当我在Postman rest client上尝试时,可以登录。

code for Csrf token: Csrf令牌的代码:

func getCSRFtoken() (key string) {
    doc, err := goquery.NewDocument(loginUrl)
    if err != nil {
        log.Fatal(err)
    }
    types := doc.Find("input")
    for node := range types.Nodes {
        singlething := types.Eq(node)

        hidden_input, _ := singlething.Attr("type")

        if hidden_input == "hidden" {
            key, _ := singlething.Attr("value")
            return key
        }
    }
    return ""
}

goquery.NewDocument is a http.Get() goquery.NewDocument是一个http.Get()

My question now is, how does the URL get's formatted from the library 我现在的问题是,如何从库中格式化URL

Maybe you would be better off using: (c *Client)PostForm(url string, data url.Values) (resp *Response, err error) 也许您最好使用: (c *Client)PostForm(url string, data url.Values) (resp *Response, err error)

from net/http like http://play.golang.org/p/8D6XI6arkz 来自net / http,例如http://play.golang.org/p/8D6XI6arkz

With the params in url.Values (instead of concatenating the strings, like you are doing now.) 使用url.Values中的参数(而不是像现在那样串联字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM