/
login.go
35 lines (32 loc) · 951 Bytes
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package vjudge
import (
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/goodguy-project/goodguy-crawl/util/errorx"
"github.com/goodguy-project/goodguy-crawl/util/httpx"
)
func login(username, password string) (*http.Client, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, errorx.New(err)
}
client := &http.Client{Jar: jar}
request, err := http.NewRequest("POST", "https://vjudge.net/user/login",
strings.NewReader(fmt.Sprintf("username=%s&password=%s", url.QueryEscape(username), url.QueryEscape(password))))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
if err != nil {
return nil, errorx.New(err)
}
response, _, err := httpx.SendRequest[[]byte]("vjudge", client, request)
if err != nil {
return nil, errorx.New(err)
}
if r := string(response); r != "success" {
return nil, errorx.New(errors.New(r))
}
return client, nil
}