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
+7.7k Golang : Function wrapper that takes arguments and return result example
+3k Golang : Valued expressions and functions example
+4.7k CloudFlare : Another way to get visitor's real IP address
+10.5k Golang : Validate email address
+10.6k Golang : Generate Code128 barcode
+9.4k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+25.1k Golang : Calculate percentage change of two values
+6.2k Golang : Randomize letters from a string example
+4.2k Unix/Linux : Get reboot history or check when was the last reboot date
+8.1k Javascript : Read/parse JSON data from HTTP response
+25.6k Golang : How to verify uploaded file is image or allowed file types
+26.9k Golang : Interpolating or substituting variables in string examples