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
+4.6k MariaDB/MySQL : How to get version information
+22.2k Golang : Match strings by wildcard patterns with filepath.Match() function
+7.8k Golang : Generate human readable password
+18k Golang : Get all upper case or lower case characters from string example
+11.3k Golang : Characters limiter example
+12.7k Android Studio : Highlight ImageButton when pressed on example
+5k Google : Block or disable caching of your website content
+5.9k Unix/Linux : How to open tar.gz file ?
+5.4k Python : Delay with time.sleep() function example
+9.4k Golang : Find the length of big.Int variable example
+36.5k Golang : Save image to PNG, JPEG or GIF format.
+10.4k Golang : cannot assign type int to value (type uint8) in range error