Using Haskell's SimpleHTTP with a Proxy?
I'm trying to figure out 开发者_Python百科how make a basic GET request using Network.HTTP.simpleHTTP that will use a proxy.
I have tried setting the http_proxy environment variable, and I think I understand how to make a Proxy using Network.HTTP.Proxy. But there seems to be nothing in the API on Hackage for how to set a simpleHTTP call or Request type to use a proxy.
The current code I have is:
import Network.HTTP
import Network.HTTP.Proxy
import Data.Maybe
main = do
let proxy = fromJust $ parseProxy "proxyserver.foo.com:80"
x <- simpleHTTP (getRequest "http://www.google.com") >>= getResponseBody
print x
From the documentation for simpleHTTP
:
simpleHTTP req
transmits theRequest req
by opening a direct, non-persistent connection to the HTTP server... If you have to mediate the request via an HTTP proxy, you will have to normalize the request yourself. Or switch to usingNetwork.Browser
instead.
I'm not really sure what the authors mean by "normalize the request yourself" here. I don't see how it would be possible with the simpleHTTP
in Network.HTTP
—I think you'd have to use the one in Network.HTTP.HandleStream
.
So why not just use Network.Browser
instead? It's very handy:
import Control.Applicative ((<$>))
import Data.Maybe (fromJust)
import Network.Browser
import Network.HTTP
import Network.HTTP.Proxy (parseProxy)
main = do
rsp <- browse $ do
setProxy . fromJust $ parseProxy "127.0.0.1:8118"
request $ getRequest "http://www.google.com"
print $ rspBody <$> rsp
精彩评论