var (
requestGetURLNoParams string = "http://httpbin.org/get"
requestGetURL string = "http://httpbin.org/get?a=a&b=b&c=ccc"
imageURL string = "http://httpbin.org/image"
)
// 基本get请求
func basicGet() {
resp, err := http.Get(requestGetURLNoParams)
if err != nil {
log.Println("err")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("err")
}
fmt.Println(string(b))
}
请求参数放到url.Values{}
// get请求参数放到 "net/url"
func basicGetURLParams() {
params := url.Values{}
parseURL, err := url.Parse(requestGetURLNoParams)
if err != nil {
log.Println("err")
}
params.Set("aaa", "aaa")
params.Set("age", "23")
//如果参数中有中文参数,这个方法会进行URLEncode
parseURL.RawQuery = params.Encode()
urlPathWithParams := parseURL.String()
resp, err := http.Get(urlPathWithParams)
if err != nil {
log.Println("err")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("err")
}
fmt.Println(string(b))
}
// 可以设置请求头 添加cookie
func basicGetHeader() {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, requestGetURLNoParams, nil)
if err != nil {
log.Println("err")
}
// 添加请求头
req.Header.Add("Content-type", "application/json;charset=utf-8")
req.Header.Add("header", "header")
// 添加cookie
cookie1 := &http.Cookie{
Name: "aaa",
Value: "aaa-value",
}
req.AddCookie(cookie1)
// 发送请求
resp, err := client.Do(req)
if err != nil {
log.Println("err")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("err")
}
fmt.Println(string(b))
}// 可以设置请求头 添加cookie
func basicGetHeader() {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, requestGetURLNoParams, nil)
if err != nil {
log.Println("err")
}
// 添加请求头
req.Header.Add("Content-type", "application/json;charset=utf-8")
req.Header.Add("header", "header")
// 添加cookie
cookie1 := &http.Cookie{
Name: "aaa",
Value: "aaa-value",
}
req.AddCookie(cookie1)
// 发送请求
resp, err := client.Do(req)
if err != nil {
log.Println("err")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("err")
}
fmt.Println(string(b))
}
接收文件–ioutil.WriteFile
// ioutil.ReadAll(resp.Body) 先将所有的响应读出来放到内存中。如果文件太大,那么就会消耗很多内存
func basicGetDownloadFile() {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, imageURL, nil) // strings.NewReader(data)
if err != nil {
log.Println("err")
}
resp, err := client.Do(req)
if err != nil {
log.Println("err")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("err")
}
ioutil.WriteFile("./images/img.jpg", b, 0644)
}// ioutil.ReadAll(resp.Body) 先将所有的响应读出来放到内存中。如果文件太大,那么就会消耗很多内存
func basicGetDownloadFile() {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, imageURL, nil) // strings.NewReader(data)
if err != nil {
log.Println("err")
}
resp, err := client.Do(req)
if err != nil {
log.Println("err")
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("err")
}
ioutil.WriteFile("./images/img.jpg", b, 0644)
}
接收文件–io.Copy
// io.Copy() 省去了先把内容读取到内存,然后将内存中的内容写到文件
func basicGetDownloadFileIoCopy() {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, imageURL, nil)
if err != nil {
log.Println("err")
}
resp, err := client.Do(req)
if err != nil {
log.Println("err")
}
defer resp.Body.Close()
// 创建一个文件用于保存
dest, err := os.Create("./images/img.jpg")
if err != nil {
log.Println("err")
}
defer dest.Close()
// 然后将响应流和文件流对接起来
_, err = io.Copy(dest, resp.Body)
if err != nil {
log.Println("err")
}
}
//基本的GET请求
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
// HTTP get请求
func httpget(ch chan int){
resp, err := http.Get("http://localhost:8000/rest/api/user")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
fmt.Println(resp.StatusCode)
if resp.StatusCode == 200 {
fmt.Println("ok")
}
ch <- 1
}
// 主方法
func main() {
start := time.Now()
// 注意设置缓冲区大小要和开启协程的个人相等
chs := make([]chan int, 2000)
for i := 0; i < 2000; i++ {
chs[i] = make(chan int)
go httpget(chs[i])
}
for _, ch := range chs {
<- ch
}
end := time.Now()
consume := end.Sub(start).Seconds()
fmt.Println("程序执行耗时(s):", consume)
}