/* *golang几种post请求方式 *参考:https://www.cnblogs.com/mafeng/p/7068837.html */ package main import ( "fmt" "io/ioutil" "log" "net/http" "net/url" "regexp" "strings" ) func main() { http_get("http://www.01happy.com/demo/accept.php?id=1") //http.Get的方式请求 http_post("http://www.01happy.com/demo/accept.php", "name=cjb&id=100") //http.post的方式请求 http_PostForm("http://www.01happy.com/demo/accept.php") //http.PostForm方法 http_do_get("http://www.01happy.com/demo/accept.php?id=1") //http.do方法的GET方式进行请求 http_do_post("http://www.01happy.com/demo/accept.php", "name=cjb&id=100") //http.do方法的POST方式进行请求 http_do_get_proxy("https://2021.ip138.com/") //代理访问 } /* * 1. http.Get的方式请求 */ func http_get(urls string) string { fmt.Println("----------1. http.Get的方式请求-------------") var res_html string = "" resp, err := http.Get(urls) if err != nil { fmt.Println("http get error", err) } defer resp.Body.Close() //函数结束时关闭Body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("read error", err) } res_html = string(body) fmt.Println("返回结果:", res_html) return res_html } /* * 2. http.post的方式请求 * 使用这个方法的话,第二个参数要设置成”application/x-www-form-urlencoded”,否则post参数无法传递 */ func http_post(urls, data string) string { fmt.Println("----------2. http.post的方式请求-------------") var res_html string = "" resp, err := http.Post(urls, "application/x-www-form-urlencoded", strings.NewReader(data)) if err != nil { fmt.Println("http get error", err) } defer resp.Body.Close() //函数结束时关闭Body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("read error", err) } res_html = string(body) fmt.Println("返回结果:", res_html) return res_html } /* * 3. http.PostForm方法 */ func http_PostForm(urls string) string { fmt.Println("----------2. http.PostForm的方式请求-------------") var res_html string = "" resp, err := http.PostForm(urls, url.Values{"key": {"Value"}, "id": {"123"}}) if err != nil { fmt.Println("http from post error", err) } defer resp.Body.Close() //函数结束时关闭Body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("read error", err) // handle error } res_html = string(body) fmt.Println("返回结果:", res_html) return res_html } /* * 4. http.do方法的GET方式进行请求 * 有时需要在请求的时候设置Header、cookie, 请求体、代理、证书验证等参数之类的数据,就可以使用http.Do方法。 */ func http_do_get(urls string) string { fmt.Println("----------4. http.do方法的GET方式进行请求-------------") var res_html string = "" httpClient := &http.Client{} req, err := http.NewRequest("GET", urls, nil) //GET方式提交数据 if err != nil { fmt.Println("http do get error", err) } sp_host := strings.Split(urls, "//")[1] host := strings.Split(sp_host, "/")[0] req.Header.Set("Host", host) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0") req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Connection", "keep-alive") res, err := httpClient.Do(req) //发送请求 if err != nil { log.Fatalln("Fail: The web address cannot be accessed!\n") //网址打不开时提示错误信息! } defer res.Body.Close() //函数结束时关闭Body body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println("read error", err) } res_html = string(body) fmt.Println("返回结果:", res_html) return res_html } /* * 5. http.do方法的POST方式进行请求 * 有时需要在请求的时候设置Header、cookie, 请求体、代理、证书验证等参数之类的数据,就可以使用http.Do方法。 */ func http_do_post(urls, data string) string { fmt.Println("----------5. http.do方法的POST方式进行请求-------------") var res_html string = "" httpClient := &http.Client{} req, err := http.NewRequest("POST", urls, strings.NewReader(data)) //POST方式提交数据 if err != nil { fmt.Println("http do post error", err) } sp_host := strings.Split(urls, "//")[1] host := strings.Split(sp_host, "/")[0] req.Header.Set("Host", host) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0") req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Connection", "keep-alive") res, err := httpClient.Do(req) //发送请求 if err != nil { log.Fatalln("Fail: The web address cannot be accessed!\n") //网址打不开时提示错误信息! } defer res.Body.Close() //函数结束时关闭Body body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println("read error", err) } res_html = string(body) fmt.Println("返回结果:", res_html) return res_html } /* * 6. http.do方法的GET方式进行请求并设置代理访问 * 有时需要在请求的时候设置Header、cookie, 请求体、代理、证书验证等参数之类的数据,就可以使用http.Do方法。 */ func http_do_get_proxy(urls string) string { fmt.Println("----------6. http.do方法的GET方式进行请求并设置代理访问-------------") var res_html string = "" //设置代理 proxyURL, _ := url.Parse("http://127.0.0.1:10809") trans := &http.Transport{ Proxy: http.ProxyURL(proxyURL), } httpClient := &http.Client{ Transport: trans, } req, err := http.NewRequest("GET", urls, nil) if err != nil { fmt.Println("http do post error", err) } sp_host := strings.Split(urls, "//")[1] host := strings.Split(sp_host, "/")[0] req.Header.Set("Host", host) req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0") req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Connection", "keep-alive") res, err := httpClient.Do(req) //发送请求 if err != nil { log.Fatalln(err) //网址打不开时提示错误信息! } defer res.Body.Close() //函数结束时关闭Body body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println("read error", err) } //正则匹配返回结果中的的IP信息 reg, _ := regexp.Compile("您的iP地址是:\\[.+?\\]") sproxy := reg.FindString(string(body)) res_html = string(body) fmt.Println("使用代理后你的IP地址是:", sproxy) //打印 //fmt.Println("返回结果:", res_html) return res_html }
标签:Lang,http,string,err,res,fmt,Println,unix,互转 From: https://www.cnblogs.com/codtina/p/18418119