github.com/dolotech/hongbao@v0.0.0-20191130105438-fd59d7a5dda5/src/logic/http_client.go (about) 1 package logic 2 3 import ( 4 "data" 5 "github.com/golang/glog" 6 jsoniter "github.com/json-iterator/go" 7 "io/ioutil" 8 "math/rand" 9 "net/http" 10 "strconv" 11 "strings" 12 "time" 13 "utils/cfg" 14 ) 15 16 func Http(addr, hbid, cookie string, item data.HonbaoTb) error { 17 /*resp, err := http.Post("http://"+addr+"/index/api/kai", 18 "application/x-www-form-urlencoded", 19 strings.NewReader("hbid="+hbid)) 20 if err != nil { 21 return err 22 } 23 */ 24 25 client := &http.Client{} 26 27 req, err := http.NewRequest("POST", "http://"+addr+"/index/api/kai", strings.NewReader("hbid="+hbid)) 28 if err != nil { 29 return err 30 } 31 32 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 33 req.Header.Set("Cookie", "PHPSESSID="+cookie) 34 35 resp, err := client.Do(req) 36 if err != nil { 37 return err 38 } 39 defer resp.Body.Close() 40 body, err := ioutil.ReadAll(resp.Body) 41 if err != nil { 42 return err 43 } 44 45 j := jsoniter.Get(body) 46 47 if j.Get("code").ToString() == "ok" { 48 glog.Info("抢包成功", item) 49 } else { 50 glog.Info(j.Get("message").ToString(), item) 51 } 52 53 return nil 54 } 55 56 func GetAll(cookies cfg.Cookies) error { 57 client := &http.Client{} 58 stamp := strconv.Itoa(int(time.Now().UnixNano() / 1000000)) 59 req, err := http.NewRequest("GET", "http://"+cookies.API+"/index/API/fabao?timestamp=", strings.NewReader("timestamp="+stamp)) 60 if err != nil { 61 return err 62 } 63 r := rand.Int31n(int32(len(cookies.Cookie))) 64 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 65 req.Header.Set("Cookie", "PHPSESSID="+cookies.Cookie[r]) 66 67 resp, err := client.Do(req) 68 if err != nil { 69 return err 70 } 71 defer resp.Body.Close() 72 body, err := ioutil.ReadAll(resp.Body) 73 if err != nil { 74 return err 75 } 76 77 ParseData(body, cookies.API, cookies.Cookie[r]) 78 return nil 79 } 80 81 //http://hb.yqsyyly.cn/index/API/fabao?timestamp=1575099030713