介紹
HTTPS (Secure Hypertext Transfer Protocol)安全超文本傳輸協(xié)議,是一個(gè)安全通信通道,它基于HTTP開(kāi)發(fā)用于在客戶計(jì)算機(jī)和服務(wù)器之間交換信息。它使用安全套接字層(SSL)進(jìn)行信息交換,簡(jiǎn)單來(lái)說(shuō)它是HTTP的安全版,是使用TLS/SSL加密的HTTP協(xié)議。
HTTP和HTTPS的區(qū)別
- HTTPS是加密傳輸協(xié)議,HTTP是名文傳輸協(xié)議
- HTTPS需要用到SSL證書(shū),而HTTP不用
- HTTPS比HTTP更加安全,對(duì)搜索引擎更友好,利于SEO
- HTTPS標(biāo)準(zhǔn)端口443,HTTP標(biāo)準(zhǔn)端口80
- HTTPS基于傳輸層,HTTP基于應(yīng)用層
- HTTPS在瀏覽器顯示綠色安全鎖,HTTP沒(méi)有顯示
1.證書(shū)可以認(rèn)為就是公鑰;
2.在Https通信中,需要CA認(rèn)證中心的證書(shū)以及服務(wù)器的證書(shū)和私鑰;
3.服務(wù)器的證書(shū)是用來(lái)發(fā)送給客戶端的;
4.CA認(rèn)證中心的證書(shū)需要安裝在客戶機(jī)上,用來(lái)驗(yàn)證服務(wù)器證書(shū)的真實(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)聽(tīng)端口并提供服務(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é)果。所以需要自己編寫(xiě)發(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, // 沒(méi)餓
}
// 請(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)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- [Asp.Net Core]用Blazor Server Side實(shí)現(xiàn)圖片驗(yàn)證碼
- [Asp.Net Core] 淺談Blazor Server Side
- Ant Design Blazor 組件庫(kù)的路由復(fù)用多標(biāo)簽頁(yè)功能
- HTTP中header頭部信息詳解
- IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器
- Golang實(shí)現(xiàn)http server提供壓縮文件下載功能
- 在Golang中使用http.FileServer返回靜態(tài)文件的操作
- 基于http.server搭建局域網(wǎng)服務(wù)器過(guò)程解析
- golang的httpserver優(yōu)雅重啟方法詳解
- Blazor Server 應(yīng)用程序中進(jìn)行 HTTP 請(qǐng)求