2011-07-28 74 views
1

是否有一些Erlang的http代理? 哪個是最好的Erlang http客戶端? httpc似乎是湖的文件,例如,如何發送httpc中的cookie頭? 在mochiweb中的任何Erlang http代理或代理的例子?關於Erlang http代理

+0

這是對評論系統的測試。忽略這個。 –

回答

2

我曾經構建了一個代理,它做了upside-down-ternet之類的組合,它的組合爲webmachine和imagemagick。它基於this資源示例。

我同意亞歷山大的說法lhttpcibrowse是最好的http客戶端。我已經在生產中取得了巨大的成功。

2

我不記得用erlang編寫的任何比較有名的http代理的名字。快速谷歌搜索發現這一點:http://www.duomark.com/erlang/tutorials/proxy.html

至於HTTP客戶端,我建議嘗試lhttpc或ibrowse

如果HTTP客戶端不具有一個內置的方法在請求中指定一個cookie,通常你可以構造一個http Cookie頭並將其作爲請求http頭的一部分傳遞給http請求處理函數。

0

我理解這個問題已經回答了,但我真的要離開這裏了這一點,以防萬一有人找一些簡單,直截了當並可讀。

-module(proxy). 
-define(HTTP_PORT, 80). 
-define(DEFAULT_PROXY_PORT, 8000). 
-export([start/0,start/1,worker/1]). 


start()-> 
    start (?DEFAULT_PROXY_PORT). 

start(Port)-> 
    {ok, Listen} = gen_tcp:listen (Port, [binary, 
        {packet, 0}, 
        {reuseaddr, true}, 
        {active, false}, 
        {delay_send, false}, 
        {nodelay, true}]), 

    server_loop (Listen). 

server_loop (Listen)-> 
    {ok, Socket} = gen_tcp:accept (Listen), 
    spawn (?MODULE, worker, [Socket]), 
    server_loop (Listen). 

worker (Socket)-> 
    {ok, Request} = gen_tcp:recv (Socket, 0), 
    ListRequest = binary_to_list (Request), 
    io:format ("~p ~n", [ListRequest]), 
    [Head, URL | RequestTail] = string:tokens (ListRequest, " "), 
    ["http:", Host | URLTail] = string:tokens (URL, "/"), 
    Document = "/" ++ string:join (URLTail, "/"), 
    NewRequest = string:join ([Head, Document | RequestTail], " "), 
    io:format ("~p ~p ~p~n", [self(), Host, Document]), 
    {ok, SecondSocket} = gen_tcp:connect (Host, ?HTTP_PORT, [binary, 
          {packet, 0}, 
          {active, false}, 
          {delay_send, false}, 
          {nodelay, true}]), 
    ok = gen_tcp:send (SecondSocket, NewRequest), 
    io:format ("~p waiting...~n", [self()]), 
    Response = read_tcp (SecondSocket), 
    io:format ("~p done!~n", [self()]), 
    ok = gen_tcp:send (Socket, Response), 
    gen_tcp:close (SecondSocket), 
    gen_tcp:close (Socket). 

read_tcp (Socket)-> 
    read_tcp (Socket, []). 

read_tcp (Socket, SoFar)-> 
    case gen_tcp:recv (Socket, 0, 500) of 
    {ok, Response} -> 
     read_tcp (Socket, [Response | SoFar]); 
    {error, timeout} -> 
     lists:reverse (SoFar); 
    {error, closed} -> 
     lists:reverse (SoFar) 
    end.