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
+17.8k Golang : Parse date string and convert to dd-mm-yyyy format
+6.2k Java : Human readable password generator
+8.1k Golang : Grayscale Image
+7k Golang : Normalize email to prevent multiple signups example
+33.2k Delete a directory in Go
+9.4k Golang : Create and shuffle deck of cards example
+13k Golang : Convert IPv4 address to packed 32-bit binary format
+12.9k Golang : zlib compress file example
+20.4k Golang : Reset or rewind io.Reader or io.Writer
+14.2k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+12.5k Golang : Flush and close file created by os.Create and bufio.NewWriter example