Golang : How to check if a website is served via HTTPS
Just a short program to check if a website has redirect to HTTPS(SSL) or not. What this program does is to find the final URL of a given URL and then check to see if the final URL has https
or not.
It does not check if the connection is secured with a proper certificate or expired certificate.
Here you go!
package main
import (
"fmt"
"net/http"
"strings"
)
func main() {
// test websites
//originalURL := "//socketloop.com"
//originalURL := "http://geocities.com" -- no https
originalURL := "http://cowner.net" // -- no https
resp, err := http.Get(originalURL)
if err != nil {
fmt.Println(err)
}
// if there is any re-direction happening behind the scene
// the finalURL will be different
// in this case, there will be a re-direction to https (SSL) version
finalURL := resp.Request.URL.String()
fmt.Println("Original URL is : ", originalURL)
fmt.Println("Final URL is : ", finalURL)
// Check if served with https
fmt.Println("Is HTTPS ? : ", strings.HasPrefix(finalURL,"https"))
}
Hope this helps and happy coding!
See also : Golang : Get final or effective URL with Request.URL example
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
+32.1k Golang : Convert []string to []byte examples
+6.7k Golang : Get expvar(export variables) to work with multiplexer
+13.7k Golang : Gin framework accept query string by post request example
+13.3k Golang : Verify token from Google Authenticator App
+16k Golang : How to check if input from os.Args is integer?
+30.7k Golang : Download file example
+21.8k Golang : Use TLS version 1.2 and enforce server security configuration over client
+29.8k Golang : How to get HTTP request header information?
+18.3k Golang : Example for RSA package functions
+10.2k Golang : Detect number of faces or vehicles in a photo
+4.3k Golang : Valued expressions and functions example