我们编写一个Go程序来尝试与这个HTTPS server建立连接并通信。

//gohttps/4-https/client1.go
package main

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

func main() {
    resp, err := http.Get("https://localhost:8081")
    if err != nil {
        fmt.Println("error:", err)
        return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

运行这个client,我们得到如下错误:

$go run client1.go
error: Get https://localhost:8081: x509: certificate signed by unknown authority

此时服务端也给出了错误日志提示:
2015/04/30 16:03:31 http: TLS handshake error from 127.0.0.1:62004: remote error: bad certificate

显然从客户端日志来看,go实现的Client端默认也是要对服务端传过来的数字证书进行校验的,但客户端提示:这个证书是由不知名CA签发 的!

我们可以修改一下client1.go的代码,让client端略过对证书的校验:

//gohttps/4-https/client2.go
package main

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

func main() {
    tr := &http.Transport{
        TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    resp, err := client.Get("https://localhost:8081")

if err != nil {
        fmt.Println("error:", err)
        return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

通过设置tls.Config的InsecureSkipVerify为true,client将不再对服务端的证书进行校验。执行后的结果 也证实了这一点:
$go run client2.go
Hi, This is an example of http service in golang!

golang GET 出现 x509: certificate signed by unknown authority的更多相关文章

  1. docker push 出现:x509: certificate signed by unknown authority

    今天,部署生产的程序的时候,出现一个问题:编译正常,但是,docker 把编译好的image 推送到生产环境上去的时候,出现:x509: certificate signed by unknown a ...

  2. docker从私有镜像库pull/push镜像问题:Error response from daemon: Get https://xxxx.com/: x509: certificate signed by unknown authority

    docker从私有镜像库pull/push镜像问题:Error response from daemon: Get https://harbor.op.xxxx.com/v2/: x509: cert ...

  3. Unable to connect to the server: x509: certificate signed by unknown authority

    0x00 Problem 在使用二进制搭建 k8s 集群的过程中,使用 kubectl get 等操作时始终显示 x509: certificate signed by unknown authori ...

  4. gitlab runner使用docker报错(x509: certificate signed by unknown authority)定位

    如果gitlab runner使用docker,docker是普通配置,配置好后,runner就可以正常执行任务了. 另外一个环节Docker配置了tls加密连接,添加runner后,runner的配 ...

  5. 解决go mod或go get时`x509: certificate signed by unknown authority`错误

    一般go get私有仓库时会出现如下错误: go: xxx@v0.0.0-20190918102752-bb51b27911ca: unrecognized import path "xxx ...

  6. go使用websocket遇到dial:x509: certificate signed by unknown authority

    websocket.DefaultDialer.Dial(url, headers) 改为 websocket.Dialer{TLSClientConfig: &tls.Config{Root ...

  7. x509: certificate signed by unknown authority harbor 架构图

    默认时,client 与 Registry 的交互是通过 https 通信的.在 install Registry 时,若未配置任何tls 相关的 key 和 crt 文件,https 访问必然失败. ...

  8. Docker: Unknown – Unable to query docker version: x509: certificate is valid for

      I was playing around with Docker locally and somehow ended up with this error when I tried to list ...

  9. docker 报错:x509: certificate has expired or is not yet valid

    环境:centos 7 程序:docker 下载镜像报错: # docker pull centos Pulling repository centos FATA[0004] Get https:// ...

随机推荐

  1. ios开发 第一天

    alloc 分配内存(类方法) init 调用构造函数 id可以替代任何数据类型(不加*号) 错误现象: 2013-06-27 21:44:21.769 FieldButtonFun[3465:113 ...

  2. 跟我一起读postgresql源码(十四)——Executor(查询执行模块之——Join节点(下))

    3.HashJoin 节点 postgres=# explain select a.*,b.* from test_dm a join test_dm2 b on a.xxx = b.xxx; QUE ...

  3. xampp使用中mysql端口被占用问题的解决方案

    如果在安装XAMPP前本机已经安装了mysql,并且添加了Windows服务中 使用xampp时,两个Mysql在Windows服务中有冲突 这意味着你之前在电脑上使用过mysql,路径.端口都被占用 ...

  4. linux_samba服务安装

    什么是samba服务? 用于Windows和linux系统之间实现共享文件的目的服务 如何配置其服务? Linux端: 搭建服务 1. 安装samba yum install -y samba 2. ...

  5. 《共享库PATH与ld.so.conf简析》

    这是摘抄<共享库PATH与ld.so.conf简析>1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个 ...

  6. PHP面试题超强总结(PHP中文网)

    PHP面试基础题目 1.双引号和单引号的区别 双引号解释变量,单引号不解释变量 双引号里插入单引号,其中单引号里如果有变量的话,变量解释 双引号的变量名后面必须要有一个非数字.字母.下划线的特殊字符, ...

  7. ios中pch文件的创建与配置

     PCH文件(Precompile Prefix Header File),也就是预编译头文件,其作用就是,方便你一次性导入在多个文件中同时用到的头文件.宏或者URL地址等(全局使用),可以有效的帮你 ...

  8. 获取IP Address

    public string GetUserIp() { var visitorsIpAddr = string.Empty; if (System.Web.HttpContext.Current.Re ...

  9. CSS中的选择器之类选择器和id选择器

    1.css中的选择器: 1.类选择器,又叫class选择器 2.id选择器 3.html元素选择器(又叫标签选择器) 4.通配符选择器 5.伪类选择器 6.组合选择器(多元素选择器,子元素选择器,后代 ...

  10. 安装golang的mongodb驱动mgo速记

    这里介绍的方法只适用于Centos平台,测试版本为centos 6.5 下载源码安装实在麻烦,这里采用比较简单的方法给GO安装mongodb驱动 安装mgo之前,需要先安装bzr yum -y ins ...