[英]Get the status code of a webpage in Haskell
我正試圖找到一種方法來檢查Haskell中是否存在網頁。 服務器只是HTTP2 / HTTPS,我正在嘗試檢查該服務器應用程序中是否存在該頁面。
是否有任何Haskell軟件包具有良好的文檔來檢查狀態代碼是200還是404? 使用強大的HTTPS和HTTP2服務器?
這里我目前有什么與http-conduit,但我收到了奇怪的例外(TlsExceptionHostPort(HandshakeFailed(Error_Protocol(“期待服務器問候,得到警報:[(AlertLevel_Fatal,HandshakeFailure)]”,True,HandshakeFailure)))“thibaud.dauce .fr“443和StatusCodeException)。
... other imports
import qualified Network.HTTP.Conduit as HTTP
... other types
type AppM = ReaderT Config (EitherT ServantErr IO)
newComment :: String -> OneComment -> AppM Int64
newComment baseUrl oneComment = do
time <- liftIO getCurrentTime
response <- HTTP.withManager $ \manager -> do
request <- HTTP.parseUrl $ url oneComment
HTTP.httpLbs request manager
case (statusIsSuccessful $ HTTP.responseStatus response, startswith baseUrl (url oneComment)) of
(_, False) -> return 0
(True, True) -> do
theNewComment <- runDb $ insert $ Comment (url oneComment) (content oneComment) time
return $ fromSqlKey theNewComment
_ -> return 0
一些使用wreq
例子
{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
import Control.Lens
import Control.Exception as E
import Network.HTTP.Client (HttpException)
test1 = do
r <- get "https://httpbin.org/get"
print $ r ^. responseStatus . statusCode
-- throws an exception
test2 = do
r <- get "https://www.google123123.com"
print $ r ^. responseStatus . statusCode
testUrl url = do
r <- get url
return $ r ^. responseStatus . statusCode
-- catching the exception
test3 = do
st <- testUrl "https://www.google123123123.com" `E.catch` handler
print st
where
handler :: HttpException -> IO Int
handler _ = return 999
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.