HTTP/2服务端与客户端例子(Go)
来源:互联网 发布:网络同步时间在线 编辑:程序博客网 时间:2024/06/04 00:52
圣诞节独自一人在家,火车票只抢到一张无座... 还是弄下Blog吧,不然Blog感觉要长草了。
这是翻以前的测试代码整理贴上来的。
一个完整的HTTP/2的服务端与客户端的Demo. 也许有人用得上。
服务端代码:
package main/*HTTP/2 服务端例子Author: XCLDate: 2016-12-25HTTP2 测试证书生成.go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost*/import ("fmt""log""net/http""time""golang.org/x/net/http2")const (_HTTP2URLBase = "https://localhost:9000"_CertFile = "../pem/cert.pem"_KeyFile = "../pem/key.pem")type handlerFunc func(w http.ResponseWriter, r *http.Request)func main() {httpMux, http2Mux := getHttpMux()go httpSrv(httpMux)httpsSrv(http2Mux)}// Mux定义 -- 设置HTTP1.1访问转向HTTP2func getHttpMux() (httpMux, http2Mux *http.ServeMux) {httpMux = http.NewServeMux()http2Mux = http.NewServeMux()x := make(map[string]handlerFunc, 0)x["/"] = Homex["/v1"] = Hello1for k, v := range x {redirectURL := http.RedirectHandler(_HTTP2URLBase+k, 307)httpMux.Handle(k, redirectURL)http2Mux.HandleFunc(k, v)}return}//HTTP服务func httpSrv(mux *http.ServeMux) {log.Fatal(http.ListenAndServe(":9001", mux))}//HTTP2服务func httpsSrv(mux *http.ServeMux) {srv := &http.Server{Addr: ":9000",ReadTimeout: 10 * time.Second,WriteTimeout: 10 * time.Second,Handler: mux,}http2.VerboseLogs = truehttp2.ConfigureServer(srv, &http2.Server{})log.Fatal(srv.ListenAndServeTLS(_CertFile, _KeyFile))}//Handler函数func Home(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "RequestURI: %s\n", r.RequestURI)fmt.Fprintf(w, "Protocol: %s\n", r.Proto)fmt.Fprintf(w, "Home")}func Hello1(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "RequestURI: %s\n", r.RequestURI)fmt.Fprintf(w, "Protocol: %s\n", r.Proto)fmt.Fprintf(w, "Hello V1")}
客户端:
package main/*HTTP/2 客户端例子Author: XCLDate: 2016-12-25测试结果:➜ client : go run client.goresp.Body: RequestURI: /v1Protocol: HTTP/2.0Hello V1*/import ("context""crypto/tls""fmt""io/ioutil""log""net/http""time""golang.org/x/net/http2")func main() {url := "https://localhost:9000/v1"client(url)}func client(url string) {tr := &http2.Transport{AllowHTTP: true, //充许非加密的链接TLSClientConfig: &tls.Config{InsecureSkipVerify: true,},}httpClient := http.Client{Transport: tr}ctx, cancel := context.WithCancel(context.TODO())time.AfterFunc(5*time.Second, func() {cancel()})req, err := http.NewRequest("GET", url, nil)if err != nil {log.Fatal(err)}req = req.WithContext(ctx)resp, err := httpClient.Do(req)if err != nil {log.Fatal(err)}defer resp.Body.Close()if resp.StatusCode != http.StatusOK {fmt.Println("resp StatusCode:", resp.StatusCode)return}body, err := ioutil.ReadAll(resp.Body)if err != nil {log.Fatal(err)}fmt.Println("resp.Body:\n", string(body))}
完毕!
Blog: http://blog.csdn.net/xcl168
0 0
- HTTP/2服务端与客户端例子(Go)
- go语言实现http服务端与客户端
- Go语言(服务器开发):简易TCP客户端与服务端
- go语言AES CFB加解密服务端与客户端互通
- 服务端+客户端 例子
- 最简单的socket客户端与服务端例子
- java socket的小例子(服务端与客户端)
- UDP 与 TCP的连接简单例子(服务端/客户端) VC
- zeroc ice 客户端与服务端通信例子(c++)
- DSAPI多功能组件编程应用-HTTP监听服务端与客户端
- C#客户端发送Http请求与服务端通信
- java RestFul 实现http接口服务端与客户端
- 文章标题C#客户端发送Http请求与服务端通信
- C#客户端发送Http请求与服务端通信
- golang udp服务端客户端例子
- HTTP客户端+服务端小工具
- c语言客户端与go语言服务端通信(网络字节序)
- MINA服务端与C++客户端通讯 2
- Git(GitHub) 003 如何 clone 非 master 分支的代码 — 切换到指定 branch分支 或者 tag版本
- 2、QT中创建一个Button
- 17 - 01 - 02 单链表的逆置
- sicily SPAM
- 51nod 1001 数组中和等于K的数对
- HTTP/2服务端与客户端例子(Go)
- Eclipse和Myeclipse断点调试
- Jspget传参数乱码问题
- 3、容器的使用
- accept()返回的套接字绑定哪个端口 新旧套接字的联系
- Jquery-层次选择器
- C语言变量的存储类型
- C语言实现顺序表
- ElasticSearch5+logstash的logstash-input-jdbc实现mysql数据同步