Http-Conduit频繁连接失败 [英] Http-Conduit frequent connection failures

查看:145
本文介绍了Http-Conduit频繁连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写将通过HTTP下载一些文件的应用程序。直到有一点,我使用下面的代码片断来下载页面主体:

  import network.HTTP 
simpleHTTP(getRequest http://www.haskell.org/)>> = getResponseBody

工作正常,但无法通过HTTPS协议建立连接。所以要解决这个问题,我已经切换到HTTP-Conduit,现在我正在使用以下代码:

  simpleHttp':: Manager  - > ;字符串 - > IO(C.Response LBS.ByteString)
simpleHttp'manager url = do
request < - parseUrl url
runResourceT $ httpLbs请求管理器

它可以连接到HTTPS,但出现了一个令人沮丧的问题。大约每隔五次连接失败,例外:

  getpics.hs:FailedConnectionExceptioni.imgur.com80 

我相信这是HTTP-Conduit问题,因为network.HTTP在相同的一组页面上工作正常(不包括https页面)。

有没有人遇到过这样的问题,并且知道解决方案或更好的方法(简单,因为这是简单的任务,应该不会超过几行代码)替代Conduit库?

解决方案

一个简单的选择是使用 curl 包。它支持HTTP,HTTPS和其他一些替代协议,以及很多选项来自定义它的行为。价格引入了构建软件包所需的 libcurl 的外部依赖。



示例:

  import Network.Curl 

main :: IO()
main = do
let addr =https://google.com/
- 对curlGetresponse_的调用需要显式类型注释。
- 使用ByteString代替String来获得更高的性能:
r< - curlGetResponse_ addr [] :: IO(CurlResponse_ [(String,String)] String)

print $ respHeaders r
putStr $ respBody r






更新:我试图复制您的问题,但一切适用于我。您可以发布一个简短的,自包含的,可编译的示例来证明问题吗?我的代码:

  import Control.Monad 
将合格的Data.Conduit导入为C
导入合格的数据。 ByteString.Lazy as LBS
import Network.HTTP.Conduit

simpleHttp'':: String - >经理 - > C.ResourceT IO(Response LBS.ByteString)
simpleHttp''url manager = do
request < - parseUrl url
httpLbs请求管理器

main :: IO ()
main = do
let url =http://i.imgur.com/
count = 100
rs< - withManager $ \m - > ; replicateM count(simpleHttp''url m)
mapM_(print。responseStatus)$ rs


I am writing application which will download some files by HTTP. Up to some point I was using following code snippet to download page body:

import network.HTTP
simpleHTTP (getRequest "http://www.haskell.org/") >>= getResponseBody

It was working fine but it could not establish connection by HTTPS protocol. So to fix this I have switched to HTTP-Conduit and now I am using following code:

simpleHttp' :: Manager -> String -> IO (C.Response LBS.ByteString)
simpleHttp' manager url = do
     request <- parseUrl url
     runResourceT $ httpLbs request manager

It can connect to HTTPS but new frustrating problem appeared. About every fifth connection fails with exception:

getpics.hs: FailedConnectionException "i.imgur.com" 80

I am convinced that this is HTTP-Conduit problem because network.HTTP was working fine on same set of pages (excluding https pages).

Have anybody met such problem and know solution or better (and simple because this is simple task which should not take more than few lines of code) alternative to Conduit library?

解决方案

One simple alternative would be to use the curl package. It supports HTTP, HTTPS and a bunch of other alternative protocols, as well as many options to customize its behavior. The price is introducing an external dependency on libcurl, required to build the package.

Example:

import Network.Curl

main :: IO ()
main = do
  let addr = "https://google.com/" 
  -- Explicit type annotation is required for calls to curlGetresponse_.
  -- Use ByteString instead of String for higher performance:
  r <- curlGetResponse_ addr [] :: IO (CurlResponse_ [(String,String)] String)

  print $ respHeaders r
  putStr $ respBody r


Update: I tried to replicate your problem, but everything works for me. Could you post a Short, Self Contained, Compilable, Example that demonstrates the problem? My code:

import Control.Monad
import qualified Data.Conduit as C
import qualified Data.ByteString.Lazy as LBS
import Network.HTTP.Conduit

simpleHttp'' :: String -> Manager -> C.ResourceT IO (Response LBS.ByteString)
simpleHttp'' url manager = do
     request <- parseUrl url
     httpLbs request manager

main :: IO ()
main = do
  let url = "http://i.imgur.com/"
      count = 100
  rs <- withManager $ \m -> replicateM count (simpleHttp'' url m)
  mapM_ (print . responseStatus) $ rs

这篇关于Http-Conduit频繁连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆