Go使用https

一、服务端

1. 生成私钥和证书

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

2. 代码

go 复制代码
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的网站

3、检测方法

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

方法二、谷歌浏览器:

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

开启了http1.1的网站

开启了http2.0的网站

四、参考资料

相关推荐
小时前端2 天前
HTTPS 页面加载 HTTP 脚本被拦?同源代理来救场
前端·https
花酒锄作田7 天前
Gin 框架中的规范响应格式设计与实现
golang·gin
郑州光合科技余经理7 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1237 天前
matlab画图工具
开发语言·matlab
dustcell.7 天前
haproxy七层代理
java·开发语言·前端
norlan_jame7 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone7 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054967 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月7 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237177 天前
C语言-数组练习进阶
c语言·开发语言·算法