golang 利用http.Client POST數據

jopen 10年前發布 | 43K 次閱讀 Golang Google Go/Golang開發

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", "

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)

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!