Go实现 简单http请求(get ,post) 多种请求方式

GET请求示例

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

type Result struct {
	Args string `json:"args"`
	Headers map[string]string `json:"headers"`
	Origin string `json:"origin"`
	Url string `json:"url"`
}
//带参数的get请求
func getParse()  {
	data := url.Values{}
	data.Set("name","test")
	data.Set("age","10")
	apiUrl := "http://httpbin.org/get"
	u,err := url.ParseRequestURI(apiUrl)
	if err !=nil{
		fmt.Println(err)
	}
	u.RawQuery = data.Encode()
	fmt.Println(u.String())
	resp,err := http.Get(u.String())
	if err !=nil{
		fmt.Println(err)
	}
	defer resp.Body.Close()
	body,err := ioutil.ReadAll(resp.Body)
	if err !=nil{
		fmt.Println(err)
	}
	fmt.Println(string(body))
}


//解析get请求的返回的json结果
func getParseToStruct()  {
	resp,err := http.Get("http://httpbin.org/get")
	if err !=nil{
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	var ret Result
	body,err := ioutil.ReadAll(resp.Body)
	if err !=nil{
		fmt.Println(err)
		return
	}
	_ =json.Unmarshal(body,&ret)
	fmt.Printf("%#v\n",ret)
}

//get 请求添加头消息
func getHttpByHeader(){
	apiUrl := "http://httpbin.org/get"
	param := url.Values{}
	param.Set("name","test")
	param.Set("age","19")
	u,_:=url.ParseRequestURI(apiUrl)
	u.RawQuery = param.Encode()


	client:=&http.Client{}
	req,err := http.NewRequest("GET",u.String(),nil)
	if err !=nil{
		fmt.Println(err)
	}
	req.Header.Add("name1","xiaoman")
	req.Header.Add("age1","3")
	resp,_ := client.Do(req)
	defer resp.Body.Close()
	body ,err := ioutil.ReadAll(resp.Body)
	if err !=nil{
		panic(err)
	}
	fmt.Println(string(body))

}



func main() {
	//getParseToStruct()
	getHttpByHeader()
}

  

post 请求示例

package main

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

//post请求
func postForm()  {
	param := url.Values{}
	param.Add("name","test")
	param.Add("age","22")
	resp,_:=http.PostForm("http://httpbin.org/post",param)
	defer resp.Body.Close()
	body,_ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

func postDemo()  {
	urlValue :=url.Values{
		"name":{"zhanfan"},
		"age":{"18"},
	}
	respData := urlValue.Encode()
	resp,_:=http.Post("http://httpbin.org/post","text/html",strings.NewReader(respData))
	//defer resp.Body.Close()
	body,_:=ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

func postBYJson()  {
	client := &http.Client{}
	m1 := make(map[string]interface{})
	m1["name"] = "test"
	m1["age"] = 12
	respdata,_:=json.Marshal(m1)
	req,_:=http.NewRequest("POST","http://httpbin.org/post",bytes.NewReader(respdata))
	resp,_:= client.Do(req)
	body,_:= ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

func postString()  {
	apiUrl :="http://httpbin.org/post"
	data := url.Values{}
	data.Add("name","test")
	data.Add("age","20")
	u,_:= url.ParseRequestURI(apiUrl)
	u.RawQuery = data.Encode()
	client := &http.Client{}
	req,_:= http.NewRequest("POST",u.String(),nil)
	resp,_ := client.Do(req)
	defer resp.Body.Close()
	body,_:= ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

func postJsonOther()  {
	m1 := make(map[string]interface{})
	m1["name"] = "test"
	m1["age"] = 12
	respdata,_:=json.Marshal(m1)
	resp,_:=http.Post("http://httpbin.org/post","application/json",bytes.NewReader(respdata))
	defer resp.Body.Close()
	body,_:=ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

func main() {
	postForm()
	//postDemo()
	postBYJson()
	//postJsonOther()
	postString()
}

  

Go实现 简单http请求(get ,post) 多种请求方式

上一篇:MVC5+EF6 入门完整教程11--MVC仓储模式的应用


下一篇:【函数分享】每日PHP函数分享(2021-1-22)