Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Haskell's SimpleHTTP with a Proxy?

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 
like image 980
devrand Avatar asked Oct 04 '10 17:10

devrand


1 Answers

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
like image 65
Travis Brown Avatar answered Oct 16 '22 03:10

Travis Brown