日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

go 网络请求篇

發(fā)布時(shí)間:2023/11/27 生活经验 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go 网络请求篇 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

---恢復(fù)內(nèi)容開始---

今天特意找了下go的網(wǎng)絡(luò)請(qǐng)求篇,get請(qǐng)求是ok的,post請(qǐng)求一直不下來,搜索了下,代碼都差不多,無法拿到post數(shù)據(jù),先整理一篇,親測(cè)可用。

針對(duì)post ,先來說下post 四種提交數(shù)據(jù)方式(Content-Type) ?鏈接

  • application/x-www-form-urlencoded ? ???key1=val1&key2=val2 ? ? ??PHP 中,$_POST['title'] 可以獲取到 title 的值,$_POST['sub'] 可以得到 sub 數(shù)組。
  • multipart/form-data ? ? 文件上傳
  • application/json ? ?在請(qǐng)求頭中 Content-Type 為 application/json 時(shí),從 php://input 里獲得原始輸入流

?

  • text/xml ? ? ? 不熟悉

?

? 我當(dāng)時(shí)測(cè)試的為php接口,然后content-type 寫成了 application/json,然后怎么post都是拿到的域名的html頁面代碼。以下為測(cè)試通過代碼

    

?

1、get請(qǐng)求

 1 package main
 2 
 3 import (
 4     "bytes"
 5     "fmt"
 6     "log"
 7     "net/http"
 8     "reflect"
 9 )
10 
11 func main() {
12 
13     url := "http://api.budejie.com/api/api_open.php?a=list&appname=baisibudejie_hd&asid=C1180CB8-F460-4385-A77C-97CD1AA83DFD&c=data&client=ipad&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=0&mac=02:00:00:00:00:00&market=&maxtime=&openudid=78336166d6a434b4cf1634410eb3b692d6d3a4ee&order=ctime&page=1&per=20000&systemversion=7.1&type=10&ver=2.0.3"
14 
15     resp, err := http.Get(url)
16 
17     if err != nil {
18         // 錯(cuò)誤處理
19         log.Println(err)
20         return
21     }
22 
23     defer resp.Body.Close() //關(guān)閉鏈接
24 
25     fmt.Printf("resp status %s,statusCode %d\n", resp.Status, resp.StatusCode)
26 
27     fmt.Printf("resp Proto %s\n", resp.Proto)
28 
29     fmt.Printf("resp content length %d\n", resp.ContentLength)
30 
31     fmt.Printf("resp transfer encoding %v\n", resp.TransferEncoding)
32 
33     fmt.Printf("resp Uncompressed %t\n", resp.Uncompressed)
34 
35     fmt.Println(reflect.TypeOf(resp.Body))
36     buf := bytes.NewBuffer(make([]byte, 0, 512))
37     length, _ := buf.ReadFrom(resp.Body)
38     fmt.Println(len(buf.Bytes()))
39     fmt.Println(length)
40     fmt.Println(string(buf.Bytes()))
41 }

2、post請(qǐng)求

package mainimport ("bytes""fmt""io/ioutil""net/http""net/url""time"
)func main() {url := "http://api.budejie.com/api/api_open.php"var data map[string]string /*創(chuàng)建集合 */data = make(map[string]string)data["a"] = "list"data["appname"] = "baisibudejie_hd"data["asid"] = "C1180CB8-F460-4385-A77C-97CD1AA83DFD"data["c"] = "data"data["client"] = "ipad"data["device"] = "ios"data["from"] = "ios"data["jbk"] = "0"data["mac"] = "02:00:00:00:00:00"data["openudid"] = "78336166d6a434b4cf1634410eb3b692d6d3a4ee"data["order"] = "ctime"data["page"] = "1"data["per"] = "20"data["systemversion"] = "7.1"data["type"] = "10"data["ver"] = "2.0.3"data["market"] = ""data["maxtime"] = ""contentType := "application/x-www-form-urlencoded"json := Post(url, data, contentType)fmt.Println(json)}//發(fā)送POST請(qǐng)求
//url:請(qǐng)求地址,data:POST請(qǐng)求提交的數(shù)據(jù),contentType:請(qǐng)求體格式,如:application/json
//content:請(qǐng)求放回的內(nèi)容
func Post(urlStr string, data map[string]string, contentType string) (content string) {postValues := url.Values{}for postKey, PostValue := range data {postValues.Set(postKey, PostValue)}postDataStr := postValues.Encode()postDataBytes := []byte(postDataStr)postBytesReader := bytes.NewReader(postDataBytes)req, err := http.NewRequest("POST", urlStr, postBytesReader)req.Header.Add("content-type", contentType)if err != nil {panic(err)}defer req.Body.Close()client := &http.Client{Timeout: 5 * time.Second}resp, error := client.Do(req)if error != nil {panic(error)}defer resp.Body.Close()result, _ := ioutil.ReadAll(resp.Body)content = string(result)return
}


  

?

?

?

---恢復(fù)內(nèi)容結(jié)束---

轉(zhuǎn)載于:https://www.cnblogs.com/lpwlpw/p/9967093.html

總結(jié)

以上是生活随笔為你收集整理的go 网络请求篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。