go ssl
// server
package main import ( "github.com/gin-gonic/gin" "log" "net/http" ) // 测试go语言证书访问 // openssl genrsa -out server.key 2048 // openssl req -nodes -new -key server.key -subj "/CN=localhost" -out server.csr // openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt func main() { r := gin.Default() r.GET("/ssl", HandleSsl) r.run(addr ) { err := r.RunTLS(addr, "server.crt", "server.key") } err := r.RunTLS(":9000", "server.crt", "server.key") if err != nil { log.Fatal(err) } } func HandleSsl(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg": "hello"}) }
// client
package main import ( "bytes" "crypto/tls" "encoding/json" "golang.org/x/net/http2" "io/ioutil" "log" "net/http" ) func main() { client := &http.Client{ Transport: &http2.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }, } marshal, err := json.Marshal("hello") if err != nil { log.Fatal(err) } buffer := bytes.NewBuffer(marshal) request, err := http.NewRequest(http.MethodGet, "https://10.18.1.72:9000/ssl", buffer) if err != nil { log.Fatal(err) } response, err := client.Do(request) if err != nil { log.Fatal(err) } all, err := ioutil.ReadAll(response.Body) if err != nil { log.Fatal(err) } log.Println(string(all)) }
posted on 2022-09-20 14:39 csuyangpeng 阅读(83) 评论(0) 编辑 收藏 举报