views:

86

answers:

1

I'm trying to figure out 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 
+2  A: 

From the documentation for simpleHTTP:

simpleHTTP req transmits the Request 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 using Network.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
Travis Brown
Thanks, that worked first try. Yeah, I saw that 'normalize' comment in the API shortly after posting but didn't have clue how to do it either.
devrand
+1 for adding a code sample :)
Daniel