Golang : 如何检查一个网站是否通过HTTPS提供服务

65 阅读1分钟

只是一个简短的程序,用于检查一个网站是否重定向到HTTPS(SSL)。这个程序所做的是找到一个给定网址的最终网址,然后检查最终网址是否有https

它并不检查连接是否有适当的证书或过期的证书。

 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"))

 }

希望这对你有帮助,并祝你编码愉快!