加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang实现http post

发布时间:2020-12-16 18:24:08 所属栏目:大数据 来源:网络整理
导读:package mainimport ("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.NewRe
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("%+vn",req)                                                         //看下发送的结构

	resp,err := client.Do(req) //发送
	defer resp.Body.Close()     //一定要关闭resp.Body
	data,_ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(data),err)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读