简体   繁体   English

简单的GoLang SSL示例

[英]Simple GoLang SSL example

Just started out with go with a Python background. 刚开始使用Python背景。

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. 我需要使用自签名证书与服务器建立SSL(HTTP GET和POST)连接,并暂时忽略安全错误。

This is dead easy in Python, but I can't seem to find a simple example. 这在Python中非常简单,但我似乎找不到简单的示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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