ssl - 简单的 GoLang SSL 示例

标签 ssl go

刚开始学习 go 语言,具有 Python 背景。

我需要使用自签名证书与服务器建立 SSL(HTTP GET 和 POST)连接并忽略(暂时)安全错误。

这在 Python 中非常简单,但我似乎找不到一个简单的示例。

谁能提供一个非常简单的例子。

更新:

好的,这就是它的工作原理,现在只需要弄清楚如何禁用证书检查!

主要包

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

最佳答案

好的排序,这是我所做的:

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

关于ssl - 简单的 GoLang SSL 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25807204/

相关文章:

security - 供内部和外部使用的 SSL 证书

go - 如何读取数组或 slice 的子集

go - 我可以重新打开已关闭的 channel 吗?

go - 从 io.ReadWriteSeeker 中删除字节(通过文件)

unit-testing - 为什么我们使用接口(interface)来模拟 Golang 方法

go - hackerrank 圆形阵列旋转 Go 运行时错误

delphi - Delphi 中的 SSL 套接字

apache - 子域和主域上的 SSL - ehost

cordova - Ionic 3 cordova-plugin-advanced-http 主机不匹配问题

rest - 无法使用损坏的 HTTPS 向页面发出 REST 请求