1 package main 2 import ( 3 "net/http" 4 "time" 5 "math/rand" 6 "io/ioutil" 7 "net/url" 8 "strings" 9 "fmt" 10 ) 11 func randString() string { 12 var s []byte 13 for l:= rand.Uint32()%10+1; l>0 ;l-- { 14 s = append(s,uint8(rand.Uint32()%26+‘a‘)); 15 } 16 //println("rand",string(s)) 17 return string(s) 18 } 19 var ch chan int 20 func post(id int) { 21 println("run post",id) 22 username := randString() 23 password := randString() 24 resp,_ := http.PostForm("http://127.0.0.1:8080/login",url.Values{"username" : {username}, "password" : {password}}) 25 println("login username:",username,"password:",password) 26 if resp == nil { 27 println("login fail") 28 return 29 } 30 println(resp.StatusCode) 31 resp.Body.Close(); 32 post1(username) 33 } 34 func post1(s string) { 35 //ch<-1 36 for true{ 37 time.Sleep(time.Second * 10) 38 resp,_ := http.PostForm("http://127.0.0.1:8080/check",url.Values{"username" : {s}}) 39 if resp == nil { 40 println("post1 no response") 41 return 42 } 43 defer resp.Body.Close(); 44 println("check online :",s) 45 } 46 } 47 func DoRequest() { 48 for i:=0; i< 100; i++ { 49 transport := http.Transport{ 50 DisableKeepAlives : true, 51 } 52 client := &http.Client{ 53 Transport : &transport, 54 } 55 56 username := randString() 57 password := randString() 58 req, err := http.NewRequest("POST", "http://127.0.0.1:8080/login", 59 strings.NewReader(url.Values{"username" : {username}, "password" : {password}}.Encode())) 60 if err != nil { 61 println("error:",err.Error()) 62 } 63 64 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 65 req.Header.Set("Cookie", "name=anny") 66 req.Header.Set("username",username) 67 req.Header.Set("password",password) 68 println("login username:",username,"password:",password) 69 resp, err := client.Do(req) 70 if err != nil { 71 println("error:",err.Error()) 72 } 73 if resp == nil { 74 println("login no response") 75 return 76 } 77 defer resp.Body.Close() 78 79 body, err := ioutil.ReadAll(resp.Body) 80 if err != nil { 81 println("error:",err.Error()) 82 } 83 84 fmt.Println(string(body)) 85 go post1(username) 86 } 87 } 88 func main() { 89 //ch=make(chan int,100000) 90 for i:=0 ;i<10;i++ { 91 go DoRequest() 92 } 93 for true { 94 // if len(ch) > 10000 { 95 // println(len(ch)) 96 // } 97 } 98 }
code
时间: 2024-10-11 00:00:20