• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

go 发送http请求

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

普通的get请求

package main

import (
    "io/ioutil"
    "fmt"
    "net/http"
)

func main() {
    res,_ :=http.Get("https://www.baidu.com/")
    defer res.Body.Close()
    body,_ := ioutil.ReadAll(res.Body)
    fmt.Print(body)
}

带参数的get请求(参数不放在url里)

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func main(){
    params := url.Values{}
    Url, _:= url.Parse("https://www.baidu.com/")
    params.Set("name","zhaofan")
    params.Set("age","23")
    //如果参数中有中文参数,这个方法会进行URLEncode
    Url.RawQuery = params.Encode()
    urlPath := Url.String()
    fmt.Println(urlPath) //等同于https://www.xxx.com?age=23&name=zhaofan
    resp,_ := http.Get(urlPath)
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

get请求添加请求头

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    client := &http.Client{}
    req,_ := http.NewRequest("GET","http://www.xxx.com",nil)
    req.Header.Add("name","zhaofan")
    req.Header.Add("age","3")
    resp,_ := client.Do(req)
   defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Printf(string(body)) }

post请求

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

func main() {
    urlValues := url.Values{}
    urlValues.Add("name","zhaofan")
    urlValues.Add("age","22")
    resp, _ := http.PostForm("http://www.xxx.com",urlValues)
defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }

post请求的另一种方式

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strings"
)

func main() {
    urlValues := url.Values{
        "name":{"zhaofan"},
        "age":{"23"},
    }
    reqBody:= urlValues.Encode()
    resp, _ := http.Post("http://www.xxx.com/post", "text/html",strings.NewReader(reqBody))
defer resp.Body.close() body,_:= ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }

post请求发送json数据

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    client := &http.Client{}
    data := make(map[string]interface{})
    data["name"] = "zhaofan"
    data["age"] = "23"
    bytesData, _ := json.Marshal(data)
    req, _ := http.NewRequest("POST","http://www.xxx.com",bytes.NewReader(bytesData))
    resp, _ := client.Do(req)
defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }

不用client
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    data := make(map[string]interface{})
    data["name"] = "zhaofan"
    data["age"] = "23"
    bytesData, _ := json.Marshal(data)
    resp, _ := http.Post("http://www.xxx.com","application/json", bytes.NewReader(bytesData))
defer resp.Body.close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) }

 说白了,我们记住http.get 和http.post就可以了


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go语言用堆排序的方法进行一千万个int随机数排序.发布时间:2022-07-10
下一篇:
Go语言基础之变量发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap