英文:
Submitting form with golang http library
问题
好的,我会为你翻译这段内容。以下是翻译的结果:
好的,我目前正在尝试使用自己的网络爬虫登录学校的网站。尽管他们对登录进行了一些保护。首先,我发送一个Get请求到网站,以获取隐藏输入字段中的令牌。然后,我将该令牌用于下一个Post请求,以登录到该URL!但是由于某种原因,HTTP响应是我无法重新提交表单。但是,使用Postman REST客户端(Chrome插件)进行相同操作时,我可以登录!
当我尝试提交一个表单到这个URL时:
postLoginUrl = "?username=%s&password=%s&submit=inloggen&_eventId=submit&credentialsType=ldap<=%s"
loginUrl = "https://login.hro.nl/v1/login"
其中%s是填入的凭据
req, err := client.Post(loginUrl, "application/x-www-form-urlencoded", strings.NewReader(uri))
我得到的响应是无法重新提交表单。
但是当我使用Postman REST客户端时,我可以登录。
获取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
是一个 http.Get()
我的问题是,这个库是如何格式化URL的?
英文:
Oke, I'm currently trying to login in to my school website, with my own 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! But for some reason the http response is that I cannot resubmit the form. But with doing the same in Postman rest client (chrome plugin) I can login!
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
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.
code for Csrf token:
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()
My question now is, how does the URL get's formatted from the library
答案1
得分: 1
也许你最好使用以下方式:
(c *Client)PostForm(url string, data url.Values) (resp *Response, err error)
从 net/http 中引入,就像这个示例中的 http://play.golang.org/p/8D6XI6arkz
使用 url.Values 中的参数(而不是像你现在这样拼接字符串)。
英文:
Maybe you would be better off using:
(c *Client)PostForm(url string, data url.Values) (resp *Response, err error)
from net/http like http://play.golang.org/p/8D6XI6arkz
With the params in url.Values (instead of concatenating the strings, like you are doing now.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论