Golang GET appears x509:certificate signed by unknown authority

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.