2016-05-07 26 views
1

我試圖找到一種方法來檢查網頁是否存在於Haskell中。服務器只有HTTP2/HTTPS,我試圖檢查頁面是否存在於servant應用程序中。獲取Haskell中網頁的狀態代碼

是否有任何Haskell包具有良好的文檔來檢查狀態碼是200還是404?並使用強大的HTTPS和HTTP2服務器? (握手失敗(Error_Protocol(「期待服務器你好,得到警報:[(AlertLevel_Fatal,HandshakeFailure)]」,True,HandshakeFailure)))這是我目前與http-conduit,但我收到奇怪的異常(TlsExceptionHostPort 「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 
+1

一些例子來看看['wreq'包(http://www.serpentine.com/wreq/) – ErikR

回答

3

使用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 
+0

我用你的代碼,我得到一個'TlsExceptionHostPort(HandshakeFailed(Error_Protocol(「期待服務器hello,獲得警報:[(AlertLevel_Fatal,HandshakeFailure)],True,HandshakeFailure)))」thibaud.dauce.fr「443',即使我的網站是一個完全有效的HTTPS證書... –

+0

'wreq'使用['hs-tls'](https://github.com/vincenthz/hs-tls),並且某些類型的證書和密碼存在問題。我將從github repo構建最新版本,並運行'tis-simpleclient'程序以獲取更多診斷信息。還要檢查問題跟蹤器 - 特定ISP提供商存在問題。 – ErikR

+0

前幾天發佈了一個問題以獲得更多信息https://github.com/vincenthz/hs-tls/issues/142。 'tls-simpleclient'似乎也失敗了...感謝您的幫助,並再次獲取您的代碼示例! –