美文网首页
Go使用https

Go使用https

作者: ljh123 | 来源:发表于2024-06-10 12:20 被阅读0次

一、服务端

1. 生成私钥和证书

以上两个步骤,参考:Go http2 和 h2c

2. 代码

package main

import (
    "log"
    "net/http"
    "time"
    "golang.org/x/net/http2"
)

const idleTimeout = 5 * time.Minute
const activeTimeout = 10 * time.Minute

func main() {
    var srv http.Server
    //http2.VerboseLogs = true
    srv.Addr = ":8080"
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hello http2"))
    })
    http2.ConfigureServer(&srv, &http2.Server{})
    go func() {
        log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
    }()
    select {}
}

二、客户端

三、浏览器

1、开启了h2的网站

为了方便介绍后面的检测h2的插件和检测有没有开h2的方法,这边给出了几个已经开启了h2的一些网站地址

2、检测插件

  • HTTP/2 and SPDY indicator

当访问开启h2的网站

3281371073.png

3、检测方法

方法一、HTTP/2 and SPDY indicator插件

方法二、谷歌浏览器:

  • Console
    • window.chrome.loadTimes()
    • window.chrome.loadTimes().connectionInfo
    • window.chrome.loadTimes().npnNegotiatedProtocol

开启了http1.1的网站

E:\数据\人生\obsidian\notes\资料库\附件

3281371073.png

开启了http2.0的网站

2010633536.png

四、参考资料

相关文章

网友评论

      本文标题:Go使用https

      本文链接:https://www.haomeiwen.com/subject/srbiqjtx.html