-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.get.redirect.url.jd.go
51 lines (47 loc) · 1.37 KB
/
url.get.redirect.url.jd.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package util
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
// 可还原的链接:
// https://u.jd.com/K7oUzZ
func GetRedirectUrlJD(str string, isClear bool) (retText string, err error) {
req, err := http.NewRequest("GET", str, nil)
if err != nil {
return
}
// req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr, Timeout: 30 * time.Second}
// httpClient := &http.Client{}
// client.Timeout = app.Timeout
response, err := client.Do(req)
if err != nil {
return
}
defer response.Body.Close()
// fmt.Println("response.StatusCode", response.StatusCode)
if response.StatusCode != 200 {
err = fmt.Errorf("请求错误:%d", response.StatusCode)
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}
// strings.TrimSpace(s string)会返回一个string类型的slice,并将最前面和最后面的ASCII定义的空格去掉,中间的空格不会去掉,如果遇到了\0等其他字符会认为是非空格。
// retBody := strings.TrimSpace(string(body))
// 清除网页内容中的空格
retBody := strings.Replace(string(body), " ", "", -1)
retText = BetweenStr(retBody, "hrl='", "'")
if retText != "" {
retText, err = GetRedirectUrl(retText, false)
}
return
}