Golang簡(jiǎn)單實(shí)現(xiàn)http的server端和client端
介紹
HTTPS (Secure Hypertext Transfer Protocol)安全超文本傳輸協(xié)議,是一個(gè)安全通信通道,它基于HTTP開發(fā)用于在客戶計(jì)算機(jī)和服務(wù)器之間交換信息。它使用安全套接字層(SSL)進(jìn)行信息交換,簡(jiǎn)單來說它是HTTP的安全版,是使用TLS/SSL加密的HTTP協(xié)議。
HTTP和HTTPS的區(qū)別
- HTTPS是加密傳輸協(xié)議,HTTP是名文傳輸協(xié)議
- HTTPS需要用到SSL證書,而HTTP不用
- HTTPS比HTTP更加安全,對(duì)搜索引擎更友好,利于SEO
- HTTPS標(biāo)準(zhǔn)端口443,HTTP標(biāo)準(zhǔn)端口80
- HTTPS基于傳輸層,HTTP基于應(yīng)用層
- HTTPS在瀏覽器顯示綠色安全鎖,HTTP沒有顯示
1.證書可以認(rèn)為就是公鑰;
2.在Https通信中,需要CA認(rèn)證中心的證書以及服務(wù)器的證書和私鑰;
3.服務(wù)器的證書是用來發(fā)送給客戶端的;
4.CA認(rèn)證中心的證書需要安裝在客戶機(jī)上,用來驗(yàn)證服務(wù)器證書的真實(shí)性
http server端
http 服務(wù)器
package main import ( "log" "net/http" "time" ) func main() { // 創(chuàng)建路由器 mux := http.NewServeMux() // 設(shè)置路由規(guī)則 mux.HandleFunc("/hello", sayHello) // 創(chuàng)建服務(wù)器 server := &http.Server{ Addr: ":1210", WriteTimeout: time.Second * 3, Handler: mux, } // 監(jiān)聽端口并提供服務(wù) log.Println("starting httpserver at http:localhost:1210") log.Fatal(server.ListenAndServe()) } func sayHello(w http.ResponseWriter, r *http.Request) { time.Sleep(1 * time.Second) w.Write([]byte("hello hello, this is httpserver")) }
啟動(dòng)服務(wù)器
$ go run demo/base/http/server/server.go 2021/05/31 22:26:35 starting httpserver at http:localhost:1210
使用 瀏覽器 或者 命令行測(cè)試一下:
$ curl -v http://localhost:1210/hello * Trying ::1:1210... * Connected to localhost (::1) port 1210 (#0) > GET /hello HTTP/1.1 > Host: localhost:1210 > User-Agent: curl/7.69.1 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Date: Mon, 31 May 2021 14:28:28 GMT < Content-Length: 31 < Content-Type: text/plain; charset=utf-8 < * Connection #0 to host localhost left intact hello hello, this is httpserver
如上所示:這就是我們服務(wù)端返回的內(nèi)容 hello hello, this is httpserver
http 客戶端
為什么需要客戶端
在多項(xiàng)目、微服務(wù)的場(chǎng)景下,項(xiàng)目服務(wù)之間的互相通信并不像。使用瀏覽器、命令行輸入域名返回結(jié)果。所以需要自己編寫發(fā)起 http 請(qǐng)求的客戶端,實(shí)現(xiàn)項(xiàng)目服務(wù)之間的通信
package main import ( "fmt" "io/ioutil" "net" "net/http" "time" ) func main() { // 創(chuàng)建連擊池 transport := &http.Transport{ DialContext: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).DialContext, MaxIdleConns: 100, // 最大空閑連接數(shù) IdleConnTimeout: 90 * time.Second, // 空閑超時(shí)時(shí)間 TLSHandshakeTimeout: 10 * time.Second, // tls 握手超時(shí)時(shí)間 ExpectContinueTimeout: 1 * time.Second, // 100-continue狀態(tài)碼超時(shí)時(shí)間 } // 創(chuàng)建客戶端 client := &http.Client{ Transport: transport, Timeout: 30 * time.Second, // 沒餓 } // 請(qǐng)求數(shù)據(jù) resp, err := client.Get("http://localhost:1210/hello") if err != nil { panic(err) } defer resp.Body.Close() // 讀取數(shù)據(jù) bds, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } fmt.Println(string(bds)) }
運(yùn)行服務(wù)測(cè)試一下go run demo/base/http/server/server.go,返回 服務(wù)端響應(yīng)內(nèi)容 hello hello, this is httpserver
到此這篇關(guān)于Golang簡(jiǎn)單實(shí)現(xiàn)http的server端和client端的文章就介紹到這了,更多相關(guān)golang http client 和server 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Golang數(shù)據(jù)競(jìng)態(tài)
本文主要介紹了淺談Golang數(shù)據(jù)競(jìng)態(tài),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02go語言beego框架jwt身份認(rèn)證實(shí)現(xiàn)示例
這篇文章主要為大家介紹了go語言beego框架jwt身份認(rèn)證實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04golang 使用time包獲取時(shí)間戳與日期格式化操作
這篇文章主要介紹了golang 使用time包獲取時(shí)間戳與日期格式化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12golang并發(fā)執(zhí)行的幾種方式小結(jié)
本文主要介紹了golang并發(fā)執(zhí)行的幾種方式小結(jié),主要包括了Channel,WaitGroup ,Context,使用這三種機(jī)制中的一種或者多種可以達(dá)到并發(fā)控制很好的效果,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08