简体   繁体   中英

Simple GoLang SSL example

Just started out with go with a Python background.

I need to make an SSL (HTTP GET and POST) connection to server with a self signed certificate and ignore (for now) the security errors.

This is dead easy in Python, but I can't seem to find a simple example.

Can somebody provide a really simple example.

UPDATE:

OK, so this is how it works, just need to work out how to disable the cert checking for now!

package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
    "os"
)

func main() {
    response, err := http.Get( "https://golang.com/")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(1)
        }
        fmt.Printf("%s\n", string(contents))
    }
}

OK Sorted, here's what I did:

package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
    "os"
    "crypto/tls"
)

func main() {

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify : true},
    }

    client := &http.Client{Transport: tr}

    response, err := client.Get( "https://80.84.50.156/VEDsdk/")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(1)
        }
        fmt.Printf("%s\n", string(contents))
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM