您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Golang語言HTTP客戶端的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
package task import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "net/url" "strings" "time" ) var Client http.Client = clients() // Res 模擬響應結構 // @Description: type Res struct { Have string `json:"Have"` } // Get 獲取GET請求 // @Description: // @param uri // @param args // @return *http.Request func Get(uri string, args map[string]interface{}) *http.Request { uri = uri + "?" +ToValues(args) request, _ := http.NewRequest("get", uri, nil) return request } // PostForm POST接口form表單 // @Description: // @param path // @param args // @return *http.Request func PostForm(path string, args map[string]interface{}) *http.Request { request, _ := http.NewRequest("post", path, strings.NewReader(ToValues(args))) return request } // PostJson POST請求,JSON參數 // @Description: // @param path // @param args // @return *http.Request func PostJson(path string, args map[string]interface{}) *http.Request { marshal, _ := json.Marshal(args) request, _ := http.NewRequest("post", path, bytes.NewReader(marshal)) return request } // ToValues 將map解析成HTTP參數,用于GET和POST form表單 // @Description: // @param args // @return string func ToValues(args map[string]interface{}) string { if args != nil && len(args) > 0 { params := url.Values{} for k, v := range args { params.Set(k, fmt.Sprintf("%v", v)) } return params.Encode() } return "" } // Response 獲取響應詳情,默認[]byte格式 // @Description: // @param request // @return []byte func Response(request *http.Request) []byte { res, err := Client.Do(request) if err != nil { return nil } body, _ := ioutil.ReadAll(res.Body) // 讀取響應 body, 返回為 []byte defer res.Body.Close() return body } // clients 初始化請求客戶端 // @Description: // @return http.Client func clients() http.Client { return http.Client{ Timeout: time.Duration(5) * time.Second, //超時時間 Transport: &http.Transport{ MaxIdleConnsPerHost: 5, //單個路由最大空閑連接數 MaxConnsPerHost: 100, //單個路由最大連接數 IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, }, } } // ParseRes 解析響應 // @Description: // @receiver r // @param res func (r *Res) ParseRes(res []byte) { json.Unmarshal(res, r) } // ParseRes 解析響應,將[]byte轉成傳入對象 // @Description: // @param res // @param r // func ParseRes(res []byte, r interface{}) { json.Unmarshal(res, r) }
package main import ( "fmt" "funtester/src/task" "io" "log" "net/http" "os" "time" ) const ( a = iota b c d e ) func init() { os.Mkdir("./log/", 0777) os.Mkdir("./long/", 0777) file := "./log/" + string(time.Now().Format("20060102")) + ".log" openFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) writer := io.MultiWriter(os.Stdout, openFile) log.SetOutput(writer) log.SetFlags(log.LstdFlags | log.Lshortfile | log.Ldate) } func main() { url := "http://localhost:12345/test" args := map[string]interface{}{ "name": "FunTester", "fun": "fdsafj", } cookie := &http.Cookie{ Name: "token", Value: "fsjej09u0934jtej", } get := task.Get(url, args) get.Header.Add("user_agent", "FunTester") get.AddCookie(cookie) response := task.Response(get) fmt.Println(string(response)) form := task.PostForm(url, args) bytes := task.Response(form) fmt.Println(string(bytes)) json := task.PostJson(url, args) res := task.Response(json) fmt.Println(string(res)) }
控制臺輸出
GOROOT=/usr/local/go #gosetup
GOPATH=/Users/oker/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/7b/0djfgf7j7p9ch_hgm9wx9n6w0000gn/T/GoLand/___go_build_funtester_src_m funtester/src/m #gosetup
/private/var/folders/7b/0djfgf7j7p9ch_hgm9wx9n6w0000gn/T/GoLand/___go_build_funtester_src_m
get請求
post請求form表單
post請求json表單Process finished with the exit code 0
依舊采用了moco_FunTester測試框架實現。
package com.mocofun.moco.main import com.funtester.utils.ArgsUtil import com.mocofun.moco.MocoServer class Share extends MocoServer { static void main(String[] args) { def util = new ArgsUtil(args) // def server = getServerNoLog(util.getIntOrdefault(0,12345)) def server = getServer(util.getIntOrdefault(0, 12345)) server.get(urlStartsWith("/test")).response("get請求") server.post(both(urlStartsWith("/test"), existForm("fun"))).response("post請求form表單") server.post(both(urlStartsWith("/test"), existParams("fun"))).response("post請求json表單") server.get(urlStartsWith("/qps")).response(qps(textRes("恭喜到達QPS!"), 1)) // server.response(delay(jsonRes(getJson("Have=Fun ~ Tester !")), 1000)) server.response("Have Fun ~ Tester !") def run = run(server) waitForKey("fan") run.stop() } }
關于“Golang語言HTTP客戶端的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。