繁体   English   中英

Haskell代理发布请求

[英]Haskell Proxy Post request

我有以下代码与GET请求的代理一起使用:

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

这个是Post,但没有代理:

main = do
  r <- post "http://www.geocodeip.com" ["IP" := Data.ByteString.Lazy.Char8.pack "79.212.82.103"]
  html <- r ^. responseBody
  print html

但是,如何通过代理发出发布请求? 我不明白。 请帮我!

如果您跟踪自己在做什么,这很简单。

我们需要使用request但向其提供POST请求而不是GET请求。 为了使这些我们使用postRequestWithBody这Hackage告诉我们有参数

postRequestWithBody :: String             | URL to POST to
                       -> String          | Content-Type of body
                       -> String          | The body of the request
                       -> Request_String  | The constructed request

因此,将request $ getRequest "http://www.google.com"替换request $ getRequest "http://www.google.com"

request $ postRequestWithBody  "http://www.geocodeip.com/" "application/x-www-form-urlencoded" "IP=79.212.82.103"

...你会很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM