go Http Post 发送文件流

来源:互联网 发布:淘宝秒杀群怎么找人 编辑:程序博客网 时间:2024/06/16 11:54
package mainimport (    "net/http"    "net/url"    "fmt"    "io/ioutil"    _ "io"    "bytes")func main()  {    postFile()}func post()  {        //这是一个Post 参数会被返回的地址        strinUrl:="http://localhost:8080/aaa"`这里写代码片`        resopne,err:=   http.PostForm(strinUrl,url.Values{"num":{"456"},"num1":{"123"}})        if err !=nil {        fmt.Println("err=",err)        }        defer func() {            resopne.Body.Close()            fmt.Println("finish")        }()        body,err:=ioutil.ReadAll(resopne.Body)        if err!=nil {            fmt.Println(" post err=",err)        }        fmt.Println(string(body))}func postFile(){    //这是一个Post 参数会被返回的地址    strinUrl:="http://localhost:8080/aaa"    byte,err:=ioutil.ReadFile("post.txt")    resopne,err  :=http.Post(strinUrl,"multipart/form-data",bytes.NewReader(byte))    if err !=nil {        fmt.Println("err=",err)    }    defer func() {        resopne.Body.Close()        fmt.Println("finish")    }()    body,err:=ioutil.ReadAll(resopne.Body)    if err!=nil {        fmt.Println(" post err=",err)    }    fmt.Println(string(body))}

水滴石穿。这里把Go Http Post 参数的函数也贴了处理主要对比两者不同之处。

原创粉丝点击