-
Notifications
You must be signed in to change notification settings - Fork 0
/
taobaoapi.go
55 lines (47 loc) · 1.26 KB
/
taobaoapi.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
52
53
54
55
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
type IPInfo struct {
Data IP `json:"data"`
}
type IP struct {
Country string `json:"country"`
Region string `json:"region"`
City string `json:"city"`
Isp string `json:"isp"`
}
func TabaoAPI(ip string) *IPInfo {
client := &http.Client{}
req, err := http.NewRequest("POST", "http://ip.taobao.com/outGetIpInfo", strings.NewReader("ip="+ip+"&accessKey=alibaba-inc"))
checkErr(err)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.AddCookie(&http.Cookie{Name: "test",Value: "test"})
resp, err := client.Do(req)
checkErr(err)
defer resp.Body.Close()
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
var result IPInfo
if err := json.Unmarshal(out, &result); err != nil {
return nil
}
return &result
}
// 基于淘宝api的物理地址及运营商查询
func GetAdr_TB(ipadrs map[string]string,ip string)string {
if _, ok :=ipadrs[ip];ok{
return ipadrs[ip]
}else {
result:=TabaoAPI(ip)
adr:= result.Data.Country+" "+result.Data.Region+" "+result.Data.City+" "+result.Data.Isp
ipadrs[ip]=adr
return adr
}
}