refresh 刷新 cookie 和 token
(url string)
| 88 | |
| 89 | // refresh 刷新 cookie 和 token |
| 90 | func refresh(url string) (string, error) { |
| 91 | client := &http.Client{} |
| 92 | req, _ := http.NewRequest("GET", url, nil) |
| 93 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36") |
| 94 | resp, err := client.Do(req) |
| 95 | if err != nil { |
| 96 | return "", err |
| 97 | } |
| 98 | // 获取 cookie |
| 99 | doc, err := xpath.Parse(resp.Body) |
| 100 | if err != nil { |
| 101 | return "", err |
| 102 | } |
| 103 | if token == "" || cookie == "" { |
| 104 | if temp := resp.Header.Values("Set-Cookie"); len(temp) == 0 { |
| 105 | return "", errors.New("刷新 cookie 时发生错误") |
| 106 | } else if cookie = temp[len(temp)-1]; !strings.Contains(cookie, "_session") { |
| 107 | return "", errors.New("刷新 cookie 时发生错误") |
| 108 | } |
| 109 | // 获取 token |
| 110 | defer resp.Body.Close() |
| 111 | |
| 112 | list := xpath.Find(doc, `//*[@id="shindanForm"]/input`) |
| 113 | if len(list) == 0 { |
| 114 | return "", errors.New("刷新 token 时发生错误") |
| 115 | } |
| 116 | token = list[0].Attr[2].Val |
| 117 | } |
| 118 | shindanTokenList := xpath.Find(doc, `//*[@id="shindan_token"]`) |
| 119 | if len(shindanTokenList) == 0 { |
| 120 | return "", errors.New("获取 shindan_token 时发生错误") |
| 121 | } |
| 122 | return shindanTokenList[0].Attr[3].Val, nil |
| 123 | } |
no test coverage detected
searching dependent graphs…