微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)
}

原文地址:https://www.jb51.cc/go/189198.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐