在curlGetResponse_中处理无连接情况

时间:2012-06-13 23:53:31

标签: haskell curl

使用curl时是否有方法或特殊curlGetResponse_选项来识别无连接情况?

例如,有一些curl - 命令:

withCurlDo $ respBody <$> (curlGetResponse_ "google.com" [CurlFailOnError True] ∷ IO (CurlResponse_ [(String, String)] ByteString))

如果与主机有连接,则会返回一些IO ByteString

但如果没有任何连接,则需要40秒并返回空字符串。

当连接丢失且主机无法访问时,有没有办法抛出异常或某个Left值?

1 个答案:

答案 0 :(得分:2)

您可以使用ConnectTimeout选项设置适当的超时,然后检查response code是否存在超时错误:

μ> withCurlDo $ respCurlCode <$> (curlGetResponse_ "google.com" [CurlConnectTimeout 1] ∷ IO (CurlResponse_ [(String, String)] String))
CurlOK
μ> withCurlDo $ respCurlCode <$> (curlGetResponse_ "2.2.2.2" [CurlConnectTimeout 1] ∷ IO (CurlResponse_ [(String, String)] String))
CurlOperationTimeout