golang实现http post

前端之家收集整理的这篇文章主要介绍了golang实现http post前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

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

func main() {
	v := url.Values{}
	v.Set("huifu","hello world")
	body := IoUtil.NopCloser(strings.NewReader(v.Encode())) //把form数据编下码
	client := &http.Client{}
	req,_ := http.NewRequest("POST","http://192.168.2.83:8080/bingqinggongxiang/test2",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)
}

猜你在找的Go相关文章