This is a creation in Article, where the information may have evolved or changed.
We write a go program to try to establish a connection and communicate with this HTTPS server.
Gohttps/4-https/client1.go
Package Main
Import (
"FMT"
"Io/ioutil"
"Net/http"
)
Func Main () {
RESP, err: = http. Get ("https://localhost:8081")
If err! = Nil {
Fmt. Println ("Error:", err)
Return
}
Defer resp. Body.close ()
Body, err: = Ioutil. ReadAll (resp. Body)
Fmt. Println (String (body))
}
To run this client, we get the following error:
$go Run Client1.go
Error:get Https://localhost:8081:x509:certificate signed by unknown authority
At this point the server also gives the error log prompt:
2015/04/30 16:03:31 http:tls handshake error from 127.0.0.1:62004:remote Error:bad certificate
Obviously from the client log, go implementation of the client side of the default is to the server to pass the digital certificate to verify, but the client tip: This certificate is issued by an unknown CA!
We can modify the Client1.go code so that the client side skips over the validation of the certificate:
Gohttps/4-https/client2.go
Package Main
Import (
"Crypto/tls"
"FMT"
"Io/ioutil"
"Net/http"
)
Func Main () {
TR: = &http. transport{
tlsclientconfig: &tls. Config{insecureskipverify:true},
}
Client: = &http. CLIENT{TRANSPORT:TR}
RESP, err: = client. Get ("https://localhost:8081")
If err! = Nil {
Fmt. Println ("Error:", err)
Return
}
Defer resp. Body.close ()
Body, err: = Ioutil. ReadAll (resp. Body)
Fmt. Println (String (body))
}
by setting up TLS. Config's insecureskipverify for true,client will no longer validate the certificate on the server. The result of the execution also confirms this:
$go run Client2.go
Hi, the is an example of HTTP service in golang!