1. 首页
  2. 主题
  3. Go基础

使用协程来模拟发送post请求,但是服务器端没有接收到数据?

glenshappy · · 2154 次点击
package main import ( "fmt" "io/ioutil" "net/http" "net/url" "strings" "strconv" ) func testmulti(flag int){ v := url.Values{} v.Set("name",strconv.Itoa(flag)+"-------") body := ioutil.NopCloser(strings.NewReader(v.Encode())) //把form数据编下码 client := &http.Client{} req, _ := http.NewRequest("POST", "http://new.xx.com.cn/test/testgo", body) req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") //这个一定要加,不加form的值post不过去,被坑了两小时 fmt.Printf("%+v\n", req) //看下发送的结构 resp, err := client.Do(req) //发送 defer resp.Body.Close() //一定要关闭resp.Body data, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(data), err) } func main() { for i:=0;i<10;i++ { println(i); go testmulti(i);//无法执行? } } 如上所示,为什么使用携程的时候,请求没有发出去啊?求救,急用,服务器端无法收到golang post过来的数据。。
更多评论
stevewang
耶和华是我的牧者,我必不至缺乏。
main函数应该等待其他goroutine执行完成。
#1
那些routine还没干活,main就结束了
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中