IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

Haskellで初めてのHttp通信

Httpクライアント処理を書いてみました。

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy.Char8 as BS
 
main :: IO ()
main = do
  res <- simpleHttp "http://www.example.com/"
  putStr $ BS.unpack resp

コンパイルをします。

[ec2-user@ip-xxx-xx-xx-xxx work01]$ ghc --make ./sample-http2.hs
[1 of 1] Compiling Main             ( sample-http2.hs, sample-http2.o ) [flags changed]
Linking sample-http2 ...
[ec2-user@ip-xxx-xx-xx-xxx work01]$ ls -la
合計 16732
drwxrwxr-x  2 ec2-user ec2-user     4096  47 13:27 2014 .
drwx------ 13 ec2-user ec2-user     4096  47 13:26 2014 ..
-rw-rw-r--  1 ec2-user ec2-user      617  47 12:48 2014 sample-http.hs
-rwxrwxr-x  1 ec2-user ec2-user 17102329  47 13:27 2014 sample-http2
-rw-rw-r--  1 ec2-user ec2-user     3806  47 13:27 2014 sample-http2.hi
-rw-rw-r--  1 ec2-user ec2-user      175  47 13:25 2014 sample-http2.hs
-rw-rw-r--  1 ec2-user ec2-user     4984  47 13:27 2014 sample-http2.o

リンクされるとかなり大きいファイルサイズになってしまいますね。

www.example.comにアクセスしてみます。
f:id:naotoogawa:20140407223422p:plain

[ec2-user@ip-172-31-25-234 work01]$ ./sample-http2
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 50px;
        background-color: #fff;
        border-radius: 1em;
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        body {
            background-color: #fff;
        }
        div {
            width: auto;
            margin: 0 auto;
            border-radius: 0;
            padding: 1em;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    domain in examples without prior coordination or asking for permission.</p>
    <p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

参考URL