2

我正在尝试使用 simpleHTTP 从我认为 HTTP 类中的响应类型获取响应代码(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)
4

1 回答 1

3

我想你想要的是

getResponseCode :: Result (Response ty) -> IO ResponseCode

Network.HTTP如果您使用的是 HTTP-4000.2.4 或更高版本,请从模块中获取。对于较早版本的HTTP,您rspCode显然必须在字段上进行模式匹配,类似于下面显示的rspReason字段方式。

如果您对原因感兴趣,请使用rspReason, Responseafter

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
于 2012-10-16T19:12:31.487 回答