简体   繁体   English

如何在Go中检查错误是否为TLS握手超时

[英]How to check if error is tls handshake timeout in Go

I have the following code making a request to URL and checking for errors. 我有以下代码向URL请求并检查错误。

import "net/http"

response, err := http.Head("url")

How do check if the error is due to tls handshake timeout? 如何检查错误是否是由于tls握手超时引起的? I tried the following: 我尝试了以下方法:

if err != nil {
    tlsError, ok := err.(http.tlsHandshakeTimeoutError)
    if ok {
        // handle the error
    }
}

But I cannot access the http.tlsHandshakeTimeoutError type because it is unexported. 但是我无法访问http.tlsHandshakeTimeoutError类型,因为它未导出。 How else can I check for the error type in go? 我还能如何检查go中的错误类型?

Yes tlsHandshakeTimeoutError - is not exported and the only one possibility to check on this error is: tlsHandshakeTimeoutError不导出,并且检查此错误的唯一可能性是:

import "net/url"

// ....

if urlError,ok :=  err.(*url.Error)  ; ok {
    if urlError.Error() == "net/http: TLS handshake timeout" {
        // handle the error
    }
}

Here is open ticket with discussion about it: 这是关于它的讨论的公开票:

https://github.com/golang/go/issues/15935 https://github.com/golang/go/issues/15935

By the way http errors (and tlsHandshakeTimeoutError also) provide also: 顺便说一下, http错误(还有tlsHandshakeTimeoutError还提供:

type WithTimeout interface {
   Timeout() bool
}

You can use it for you check if you don't like string comparsion. 您可以使用它来检查是否不喜欢字符串比较。 Here is example of isTemporary implementation from http2 package. 是来自http2包的isTemporary实现的示例。

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

相关问题 如何检查在 TLS/SSL 握手期间是否进行主机名验证? - how to check if hostname verification takes place during TLS/SSL handshake? 如何在Go中的测试中模拟HTTP请求的504超时错误? - How to simulate 504 timeout error for http request inside a test in Go? 如何获取这个网址? 获取 SSL 握手错误 - How to fetch this URL? Getting SSL handshake error 客户端握手错误(操作系统错误:TLSV1_ALERT_PROTOCOL_VERSION(tls_record.cc:586) 在 Flutter 应用程序中尝试执行发布/获取请求时) - Handshake error in client (OS Error: TLSV1_ALERT_PROTOCOL_VERSION(tls_record.cc:586) in flutter app when trying to do a post/get request WebLogic SSL握手错误 - WebLogic SSL Handshake error SSL握手错误:[错误1] - SSL Handshake Error: [Errno 1] 在Go中创建空闲超时? - Creating an idle timeout in Go? 如何通过文本文件中的 URL 制作 Python go,检查它们的状态码,并排除所有有 404 错误的? - How to make Python go through URLs in a text file, check their status codes, and exclude all ones with 404 error? SSLEngine:握手成功后解包时,无效的TLS填充数据 - SSLEngine: Invalid TLS padding data when unwrapping after a successful handshake 如何手动引发超时错误? - how to raise a timeout error manually?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM