<p>Currently I'm trying to crawl my own grades from my school their website. </p>
<p>I would like some help on the login part of system. Because my school uses some sort of form protection I send a get request first. To fetch the needed hidden input data.</p>
<p>after that i'm building an URL with the form data in it. This is how my school handles the submitted form.</p>
<p>But when I'm using golang everytime I submit the form I get the response from the web server saying that the form is already submitted.</p>
<p>This makes no sense to me because I didn't send any other request besides the get request to get the Token from the hidden input.</p>
<p>In postman rest client it works (getting the token & building url and submitting with a POST)</p>
<p>Someone has any idea, I'm kinda stuck on this for a time now</p>
<p>code:</p>
<p>package main</p>
<pre><code>import (
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
)
type client struct {
http.Client
}
var (
postLoginURL = "?submit=inloggen&_eventId=submit&username=%s&password=%s&credentialsType=ldap&lt=%s"
loginURL = "https://login.hro.nl/v1/login"
logoutURL = "https://login.hro.nl/v1/logout"
)
func insertCredentials(username, password, ltkey string) string {
return fmt.Sprintf(postLoginURL, username, password, ltkey)
}
func getToken() (key string) {
//Request
client := &http.Client{}
req, err := http.NewRequest("GET", loginURL, nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
//If there is no request possible, terminate the program
panic("Cannot fetch the Token, check server status!")
}
//Read content from the body
content, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
//Regex to match the value from the hidden input (Example:_s6014490C-B11F-9828-1574-467F8E575E72_c983D179C-1117-2DA6-D5FD-96331CD3CD38)
match := regexp.MustCompile("_s.{74}")
key = match.FindString(string(content))
if err != nil {
return ""
}
return
}
func (c *client) login(uri string) {
fmt.Println(loginURL + uri)
params := strings.NewReader(uri)
req, err := c.Post(loginURL, "application/x-www-form-urlencoded", params)
data, err := ioutil.ReadAll(req.Body)
fmt.Println(req)
fmt.Println(isValid(data))
fmt.Println(string(data))
defer req.Body.Close()
// error handle
if err != nil {
fmt.Printf("error = %s \n", err)
}
}
func isValid(body []byte) bool {
r := regexp.MustCompile("has been submitted already")
if r.Match(body) {
return false
}
return true
}
func main() {
cl := &client{}
cl.login(insertCredentials("XXX", "XXXX", getToken()))
}
</code></pre>
<hr/>**评论:**<br/><br/>Snavelzalf: <pre><p>Even when getting the Token from the browser and then using it as parameter it says cannot be resubmitted!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传