Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
There are times when a webmaster failed to renew the certificates from SSL connection in time or some other reason for the SSL certificates to expire. A friend of mine has a Golang program that will download data from an external website. However, the program failed recently because of the external website's expired SSL certificates.
This tutorial is a note from helping a friend today to create a custom HTTP transport client in Golang. It is to handle data retrieval for web server with expired SSL certificates by skipping security check. Basically, just a custom HTTP transport client disabled security check.
Hope you may find it useful when you encounter the similar situation in future.
Here you go!
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // ignore expired SSL certificates
}
client := &http.Client{Transport: transCfg}
response, err := client.Get("https://socketloop.com/")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer response.Body.Close()
htmlData, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(os.Stdout, string(htmlData))
}
NOTE : In a production system, it would be wise to use InsecureSkipVerify : true
parameter if the normal/secure method failed. Use it as a fallback method rather than the "normal" method. Remember to send a notification, log message or email to the system administrator if the fallback method is activated.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.2k Golang : Allow Cross-Origin Resource Sharing request
+10.3k Golang : How to flush a channel before the end of program?
+9.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+4.2k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+5k Golang : Extract unicode string from another unicode string example
+12.6k Golang : Fix image: unknown format error
+10.6k Golang : Convert decimal number(integer) to IPv4 address
+5.6k Golang : Spell checking with ispell example
+6.4k Golang : Individual and total number of words counter example
+5.8k Golang : Get expvar(export variables) to work with multiplexer
+5.9k Mac OSX : Find large files by size
+4.1k Python : Convert(cast) bytes to string example