-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclient.go
More file actions
43 lines (38 loc) · 808 Bytes
/
client.go
File metadata and controls
43 lines (38 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"net/http"
)
func main() {
pool := x509.NewCertPool()
caCertPath := "root.crt"
caCrt, err := ioutil.ReadFile(caCertPath)
if err != nil {
log.Fatal("ReadFile err:", err)
return
}
pool.AppendCertsFromPEM(caCrt)
cliCrt, err := tls.LoadX509KeyPair("client.crt", "client.key")
if err != nil {
log.Fatal("LoadX509KeyPair err:", err)
return
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
Certificates: []tls.Certificate{cliCrt},
},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://localhost:8080")
if err != nil {
log.Fatal("client error:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
log.Println(string(body))
}