我正在尝试从 HTTP 类中使用 simpleHTTP 获取响应代码(200,404 等) 到目前为止:
--how to get the http response Int from response
getStatusCode response = print 0
--this works...
--- othercode ---
rsp <- simpleHTTP (defaultGETRequest_ clean_uri)
file_buffer <- getResponseBody(rsp)
--this fails
response = (getStatusCode rsp)
最佳答案
我觉得你想要的是
getResponseCode :: Result (Response ty) -> IO ResponseCode
来自 Network.HTTP
如果您使用的是 HTTP-4000.2.4 或更高版本,则为模块。对于 HTTP
的早期版本,您显然必须在 rspCode
字段上对自己进行模式匹配,类似于下面为 rspReason
字段显示的方式。
如果您对原因感兴趣,请使用Response
的rspReason
字段, 之后
rsp <- simpleHTTP ...
你有
rsp :: Either ConnError (Response ty) -- Result is a type synonym for (Either ConnError)
并且可以访问每个原因
let reason = case rsp of
Left err -> show err -- for example
Right response -> rspReason response
putStrLn $ "Here's why: " ++ reason
关于http - Haskell SimpleHTTP 获取响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12921600/